2004-02-03 01:28 AM
How to write an unsigned char into the upper byte of an int???
2004-02-01 05:12 PM
Greeting people!!
this is probably easy but how do you do it in c? - write an unsigned char (byte) into the top byte of an int (2 bytes)? im using the cosmic compiler i guess you could do int = char; int <<= 8; is there a quicker way or is that it? thanks for your time chris2004-02-01 11:54 PM
Try using unions
union { unsigned int MyInt; unsigned char MyByte[2]; }A; Hope this helps Regards, PraveenG2004-02-02 11:27 AM
Thanks it good to learn something new
chris2004-02-03 01:28 AM
Using a shift will produce
int_var = char_var << 8; ld x,_char_var clr a ld _int_var,x ld _int_var+1,a that is, will also clear the LSB of the int you are moving the char into. Another possibility is to use pointers: *(char *)&int_var = char_var; ld a,_char_var ld _int_var,a this will leave the LSB unmodified. Hope this helps. Luca