Convert unsigned 16 bit Word
-
Hello all, This is my first post to this forum, so apologies in advance for the lack of clarity. Im trying to assign an unsigned 16 bit word to a char array i.e. two 8 bit single bytes. my value (u16 sum) contains the word value 24fa which I need to insert into the middle of a char array buff[100]. Obviously, I am concious of the fact that the array is of type char (8 bit), thus any allocation will shorten the original value e.g. buff[0] = sum //buff[0] only contains 0x24 Is there any way to change the word value to two 8 bit singular bytes for storage? Or is there another way via strcat/memcpy etc..etc Many thanks in advance. Stu
-
Hello all, This is my first post to this forum, so apologies in advance for the lack of clarity. Im trying to assign an unsigned 16 bit word to a char array i.e. two 8 bit single bytes. my value (u16 sum) contains the word value 24fa which I need to insert into the middle of a char array buff[100]. Obviously, I am concious of the fact that the array is of type char (8 bit), thus any allocation will shorten the original value e.g. buff[0] = sum //buff[0] only contains 0x24 Is there any way to change the word value to two 8 bit singular bytes for storage? Or is there another way via strcat/memcpy etc..etc Many thanks in advance. Stu
oh me too .I can not know use this forum my english is poor haha I
-
Hello all, This is my first post to this forum, so apologies in advance for the lack of clarity. Im trying to assign an unsigned 16 bit word to a char array i.e. two 8 bit single bytes. my value (u16 sum) contains the word value 24fa which I need to insert into the middle of a char array buff[100]. Obviously, I am concious of the fact that the array is of type char (8 bit), thus any allocation will shorten the original value e.g. buff[0] = sum //buff[0] only contains 0x24 Is there any way to change the word value to two 8 bit singular bytes for storage? Or is there another way via strcat/memcpy etc..etc Many thanks in advance. Stu
simonpearson wrote: Is there any way to change the word value to two 8 bit singular bytes for storage BYTE b1 = value & 0xff; BYTE b2 = (value >> 8); or WORD *pw = &value; BYTE *pb = (BYTE *)pw; // now, pb[0] is the first byte of your WORD and pb[1] is the second etc.. Cleek | Image Toolkits | Thumbnail maker
-
Hello all, This is my first post to this forum, so apologies in advance for the lack of clarity. Im trying to assign an unsigned 16 bit word to a char array i.e. two 8 bit single bytes. my value (u16 sum) contains the word value 24fa which I need to insert into the middle of a char array buff[100]. Obviously, I am concious of the fact that the array is of type char (8 bit), thus any allocation will shorten the original value e.g. buff[0] = sum //buff[0] only contains 0x24 Is there any way to change the word value to two 8 bit singular bytes for storage? Or is there another way via strcat/memcpy etc..etc Many thanks in advance. Stu
You can use a union. This union can be used to extract the individual bytes of most of the primitive data types. You can add more types as needed.
typedef union
{
double Double;
float Float;
UINT Int;
USHORT Short;
UCHAR Bytes[sizeof(double)];
} UnionBytes;UnionBytes ub;
ub.Short = your_short;
= wb.Bytes[0]; // first byte
your_string[x+1] = wb.Bytes[1]; // second byteI know you didn't ask this but you can do something similar to access the bits of a byte :
typedef union
{
UCHAR Byte;
struct
{
unsigned Bit0 : 1;
unsigned Bit1 : 1;
unsigned Bit2 : 1;
unsigned Bit3 : 1;
unsigned Bit4 : 1;
unsigned Bit5 : 1;
unsigned Bit6 : 1;
unsigned Bit7 : 1;
} Bits;
} ByteBits -
oh me too .I can not know use this forum my english is poor haha I
fisheryj wrote: haha So you find it funny if your english is poor?
-prakash