Converting a UINT16 value into a UINT8 array[2]
Asked Answered
P

6

19

This question is basically the second half to my other Question

How can I convert a UINT16 value, into a UINT8 * array without a loop and avoiding endian problems.

Basically I want to do something like this:

UINT16 value = 0xAAFF;
UINT8 array[2] = value;

The end result of this is to store value into a UINT8 array while avoiding endian conversion.

UINT8 * mArray;
memcpy(&mArray[someOffset],&array,2);

When I simply do memcpy with the UINT16 value, it converts to little-endian which ruins the output. I am trying to avoid using endian conversion functions, but think I may just be out of luck.

Pocket answered 17/8, 2009 at 17:17 Comment(0)
E
49

How about

UINT16 value = 0xAAFF;
UINT8 array[2];
array[0]=value & 0xff;
array[1]=(value >> 8);

This should deliver the same result independent of endianness.

Or, if you want to use an array initializer:

UINT8 array[2]={ value & 0xff, value >> 8 };

(However, this is only possible if value is a constant.)

Etalon answered 17/8, 2009 at 17:38 Comment(0)
D
9

There's no need for conditional compilation. You can bit-shift to get the higher and lower byte of the value:

uint16_t value = 0xAAFF;
uint8_t hi_lo[] = { (uint8_t)(value >> 8), (uint8_t)value }; // { 0xAA, 0xFF }
uint8_t lo_hi[] = { (uint8_t)value, (uint8_t)(value >> 8) }; // { 0xFF, 0xAA }

The casts are optional.

Deliverance answered 17/8, 2009 at 17:34 Comment(1)
question tag is c++, use c++ casts.Neon
K
7

Assuming that you want to have the high-order byte above the lower-order byte in the array:

array[0] = value & 0xff;
array[1] = (value >> 8) & 0xff;
Krypton answered 17/8, 2009 at 17:40 Comment(2)
the mask 0xff is unnecessary: the conversion from 16 to 8 bit integers will drop the higher byte (unsigned conversion is well-defined!)Deliverance
That's true, but I generally mask everything for clarity. Sometimes, I'll even do a bit-shift by 0 so that the lines all match up and look good vertically. Any good compiler will optimize out the unnecessary statements, and it is well worth the clarity.Krypton
S
2
union TwoBytes
{
    UINT16 u16;
    UINT8 u8[2];
};

TwoBytes Converter;
Converter.u16 = 65535;
UINT8 *array = Converter.u8;
Straley answered 17/8, 2009 at 17:47 Comment(1)
This is sensitive to endianness.Krypton
N
0

I used this thread to develop a solution that spans arrays of many sizes:

UINT32 value = 0xAAFF1188;
int size = 4;
UINT8 array[size];

int i;
for (i = 0; i < size; i++)
{
    array[i] = (value >> (8 * i)) & 0xff;
}
Norge answered 24/11, 2015 at 19:28 Comment(0)
R
-1

A temporary cast to UINT16* should do it:

((UINT16*)array)[0] = value;
Rhinencephalon answered 17/8, 2009 at 17:26 Comment(1)
This doesn't work because it's still sensitive to endianness (basically, it does the same thing as the memcpy in the question).Etalon

© 2022 - 2024 — McMap. All rights reserved.