# separating 2 bytes (truncating?)

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

## 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 on other sites

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 on other sites
short int a = 0x1234;
char hi, lo;
lo = *(char*) &a;
hi = *((char*) &a + 1);

##### 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 on other sites
That's all good and well if you don't mind that the numbers might end up backwards with some processors.

##### 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 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 on other sites
Division and multiplication will make the program slow, so reduce the time you use them to the best of your's abilities.

##### Share on other sites
Thanks for all your help guys.
Really appreciate it.

Heath

##### Share on other sites
Quote:
 Original post by Anonymous PosterDivision 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.

1. 1
Rutin
29
2. 2
3. 3
4. 4
5. 5

• 13
• 13
• 11
• 10
• 13
• ### Forum Statistics

• Total Topics
632960
• Total Posts
3009475
• ### Who's Online (See full list)

There are no registered users currently online

×