Jump to content
  • Advertisement
Sign in to follow this  
HeathyBoy

separating 2 bytes (truncating?)

This topic is 4857 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey guys, I'm trying to separate a short into two chars (2bytes to 2x 1byte). I can truncate the short to find the lower byte without any problems but how do I then change the truncated value to a 1 byte char? Hope you guys can help me out. Heath.

Share this post


Link to post
Share on other sites
Advertisement

int word =0x1234;
int byte1=word>>8&0xff;
int byte2=word&0xff;


And if you want to store the resulting values in a char, then go ahead.

Share this post


Link to post
Share on other sites
I sometimes use a union to break stuff apart


union ConvertShort
{
short _s;
char _c[2];
};

ConvertShort cs._s = 100;
char c1 = cs._c[0];
char c2 = cs._c[1];

Share this post


Link to post
Share on other sites
Using just straight math, you can do the following, thus avoiding any endian-issues I believe:

short LargeNum;
char HighPart;
unsigned char LowPart;

LargeNum = 30729;
HighPart = LargeNum / 256;
LowPart = abs(LargeNum) % 256;



Or for larger numbers than shorts:

long LargeNum;
char HighPart;
unsigned char LowParts[3];
HighPart = LargeNum / 0x01000000;
LowParts[0] = abs(LargeNum) % 0x01000000 / 0x00010000;
LowParts[1] = abs(LargeNum) % 0x00010000 / 0x00000100;
LowParts[2] = abs(LargeNum) % 0x00000100;



I'm not going to guarantee that it will work for negative numbers (especially the very 'largest' negative, -32768 in this case), but I think it should. The high part is signed, and represents the sign of the original number, and the rest are unsigned, in order to get proper use of all 8 bits. It'd be simpler if you started out with an unsigned number to begin with, but maybe not much. All your parts would be of the same type (unsigned char), and you could get rid of the abs(), but that's all I believe (assuming that the above already works fine for negatives).

Share this post


Link to post
Share on other sites

void WordToTwoBytes( unsigned short x, unsigned char * y1, unsigned char * y2 )
{
*y1 = x&255;
*y2 = x>>8;
}

smart_idiot beat me to the concent, but whatever. It works great, and its using bit operations, which are quite fast. I'm using some implicit coercion, but that shouldn't be a problem.



void IntToFourBytes( unsigned int x, unsigned char * y1, unsigned char * y2,
unsigned char * y3, unsigned char * y4 )
{
if (y1) *y1 = x&255;
if (y2) *y2 = (x>>8)&255;
if (y3) *y3 = (x>>16)&255;
if (y4) *y4 = x>>24;
}

This is a completely secure and a bit more expanded version. Granted, the ifs are a bit more expensive, but consider the freedom of saying

IntToFourBytes( 2993, 0, &z, 0, 0 ); /* get the 2nd byte from the value */

Of course, I never actually do this, I just use the bit operations directly, saving on ifs and function calls. Its worth learning, but don't get all obfuscated, you should make sure its ultimately readable.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Division and multiplication will make the program slow, so reduce the time you use them to the best of your's abilities.

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
Division and multiplication will make the program slow, so reduce the time you use them to the best of your's abilities.


Stop spreading rumors, rumorzombie.

Optimization tricks like converting "n * 256" to "n << 8" no longer work, because the compiler is smart enough to do this automatically as needed. Stop bringing up advice from the 386/Atari era please, we're programmers, not archaeologists. Well, most of us, anyways.

I'd suggest the method mentioned by smart_idiot, because of the fact that it is edian safe. The short on any architecture will come out to the same every time. Use Horus_'s or Rattrap's solution if you for some reason hate edian safety (they shouldn't be any faster with a decent optimizer). Even agony's use of the modulo operator is probably translated into the appropriate mask.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!