How to write an unsigned char into the upper byte of an int???
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2004-02-03 1:28 AM
Posted on February 03, 2004 at 10:28
How to write an unsigned char into the upper byte of an int???
Labels:
- Labels:
-
Legacy Products
4 REPLIES 4
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2004-02-01 5:12 PM
Posted on February 02, 2004 at 02:12
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 chrisOptions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2004-02-01 11:54 PM
Posted on February 02, 2004 at 08:54
Try using unions
union { unsigned int MyInt; unsigned char MyByte[2]; }A; Hope this helps Regards, PraveenGOptions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2004-02-02 11:27 AM
Posted on February 02, 2004 at 20:27
Thanks it good to learn something new
chrisOptions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2004-02-03 1:28 AM
Posted on February 03, 2004 at 10:28
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