cast byte to int
im trying to type cast from byte to int but i get a wrog result.
BYTE test[2] = {0x0A, 0x00};
short b = (short)test;
CString g;
g.Format("%d", b);
MessageBox("Number: "+g);
0x0A, 0x00 should be 10 as int. and i get -100138.
beside, the result dont change even if i change the bytes !
[338 byte] By [
yoni1993] at [2007-11-20 11:42:59]

# 1 Re: cast byte to int
if you do b = test, compiler will treat test as the address to the BYTE array and b will be initialized with that address.
If you want to assign the value instead, you need to tell the compiler explicitly that it points to a short like below:
short b = *(short*)test;
which ask the compiler to treat test as a pointer to a short, then asks the compiler to initialize b with the value ( * ) pointed to by the pointer to the short ( short * ).
# 2 Re: cast byte to int
BYTE test[2] = {0x0A, 0x00};
test above is BYTE array. Cast it to int and you get pointer to the variable
BYTE test = 0x0A;
and test here is BYTE. When casted to int, you get the value.
eero_p at 2007-11-10 22:25:24 >

# 3 Re: cast byte to int
Another option:
BYTE test[2] = {0x0A, 0x00};
char szNumDec1[256]={0};
char szNumDec2[256]={0};
_itoa( test[0], szNumDec1, 10 );
_itoa( test[1], szNumDec2, 10 );
::MessageBox(0,szNumDec1,0,0);
::MessageBox(0,szNumDec2,0,0);
Cheers