"Delali Dzirasa" <> wrote in message
news:bm1hbf$oqo$...
> I would have a number packed with its hex representation of the integer
> below is some sample code of what is being done.
>
> int value = 20; //in hex it is 0x14
>
> AddData (value);
> .
> .
> .
>
> AddData( USHORT myVal.....)
> {
> UCHAR tmp2[2];
> tmp2[1] = myVal & 0x00FF;
Suppose you pass in the decimal value 20. That's 0x14 in hex. The above
line masks off the high word (of a character, which is smaller!), so that
you are doing 0x0014 & 0x00ff, which results in 0x0014. Stored in an
unsigned char, that is 0x14, which is just what you started with: decimal
20!
> tmp2[0] = (myVal & 0xFF00) >> 8 ;
Here, you have (0x0014 & 0xff00)>>8, which results in (0x0000) >> 8, which
is 0x0000, or simply decimal 0 (zero). So, your two unsigned chars stored
in tmp2 are 0 and 20.
>
> memcpy(&myPkt[0], &tmp2, 2); // (where myPkt is a UCHAR*
> }
>
> when I check the hex dump I see that 0x0020 was packed instead of the
0x0014
> that I want. How can I set the proper flags(?) "if" that is the solution
to
> pack the hex representation of the integer?
>
I think your "hex dump" is not hex at all, but decimal, showing the first
byte as zero, and the second as 20, just like your code told it to do.
I'm not sure why you want to take an unsigned short and store it in two
unsigned characters, but that's hardly "packing", which implies reducing the
space required. What do you need in the output? Characters representing
the hex digits such as ['0','0','1','4']? A pair of unsigned char values,
one for each hex digit, such as [0,0,1,4]? Or do you really need to do this
"packng" at all? I mean, a hex dump of the original decinal value 20 will
show you 0x0014 just like you've been trying to get in the first place.
If you're trying to get the hex digits as separate values, such as
[0,0,1,4], remember that you've got 4 bytes in a short, not 2, and will need
an array of 4 unsigned char's to handle all possible unsigned short values.
And, your masks should mask off one byte at a time, not two like 0x00ff and
0xff00 do.
-Howard
|