# Thread: Splitting up int16_t to two int8_t

1. ## Splitting up int16_t to two int8_t

I try to figure out how to split up one int16_t into two int8_t... But I can't figure it out.

Well actually, I have a string containing some stuff. Then I have a representation of two chars in an int16_t that I would like to paste into a certain place of the string. How can I do this?

2. Try looking at what x % 256 and x / 256 give you.

3. I was thinking about bit shifting left* or right 8bits to move the unwanted char out of the space allocated for the int 16_t.. but then I think you'd have to typecast the 16_t variable to an 8_t ....

would this be a viable method..? if not .. why??

Code:
```footnotes:

* shifting left would require additional manipulation in order to occupy the lower 8-bits of the 16_t var.

example:

8 << var;    //move out upper 8 bits

then

8 >> var;  //return the char back to lower 8```

4. > would this be a viable method..? if not .. why?
It's OK so long as you are aware that >> on signed numbers is implementation specific.
So long as you take this into account, it's a good way to do it.

oh.. I just thought of something.. an even better way i think.. would be just to "and" the 16_t variable with a mask

pseudocode:
Code:
```16_t before;

11111111 00000000b & before;    // "and" using this mask to clear out the lower 8 bits

before >> 8;   //shift the upper bite right to occupy the lower 8 bits

8_t after_1 = static_cast<8_t>(var);   //the "upper" char is returned    :)```

For extra credit: How would you perform similar operations for a signed char

6. I found unions are kinda fun for this stuff. Not that its a great solution, but they work. Say something like:

Code:
```union ByteSplit
{
16_t int16;
8_t int8[2];
};

ByteSplit Value;
Value.int16 = OriginalValue;
//Value.int8[0] is 8 high bits
//Value.int8[1] is low 8 bits```
I used that mainly for IP addresses:
Code:
```union IP
{
int intIP;
char cIP[4];
};```
The API's want the int version, but most people can only work with the char versions.

Don't think that works for signed values but never tried it.

7. Originally Posted by Exile
I found unions are kinda fun for this stuff. Not that its a great solution, but they work. Say something like:

Code:
```union ByteSplit
{
16_t int16;
8_t int8[2];
};

ByteSplit Value;
Value.int16 = OriginalValue;
//Value.int8[0] is 8 high bits
//Value.int8[1] is low 8 bits```

Unless the system is little-endian, in which case Value.int8[0] holds the low bits and Value.int8[1] holds the high bits.

Regards,

Dave

8. Originally Posted by Dave Evans
Unless the system is little-endian, in which case Value.int8[0] holds the low bits and Value.int8[1] holds the high bits.
Does taht same problem exist for the solutions that use bit shifts? The shifts would work, but the results of the shift are switched.

9. Originally Posted by Exile
Does taht same problem exist for the solutions that use bit shifts? The shifts would work, but the results of the shift are switched.
That was my point: Bit shifts (or arithmetic with '/' and '%' operators) allow you to extract the bytes whatever order you require. (Not endian-dependent.)

The shift and arithmetic operators work on numbers in registers. Upper bits are upper bits; lower bits are lower bits.

When the individual bytes get stored in memory, the byte order is dependent on the endianness of the machine. This shows up with different results for bid-endian systems and little-endian systems using the union.

Regards,

Dave

10. I didn't know that. I figured that once the stuff was in memory it was all in the same order. Guess I lucked out and all of the stuff I used was big-endian.