Mail Archives: djgpp/1998/10/12/00:50:38
Goh Yong Kwang wrote:
>
> How do I convert an unsigned short (2 bytes) into a 2 char array?
> Does the following code does the trick?
no, that won't even make it through the compiler.
try
union {
unsigned short s;
char c[2];
} u;
u.s = 0x3132;
printf("c[0] = %c c[1] = %c\n", u.c[0], u.c[1]);
I doubt that this is what you really want, so you could do
char c[2];
unsigned short s = 0x3132;
c[0] = s >> 8;
c[1] = s & 0xff;
which allows you to determine the order in which the bytes are put into
the char array instead of depending on how a short might be stored on a
given architecture.
I am not sure this is what you really want to do either, but its what
you asked for.
- Raw text -