storing short in char

N

Neel

Hi friends,
How can I store a short into an unsigned char???

its like


unsigned char msg;

//now I want to assign 0xAB into it.

//and than want to test it
if(msg==0xAB)
printf("true");
else
printf("false");


What I did is:

unsigned char msg;
msg=(char)(0xAB<<8);

if condition....


This isnt working...
cant anyone pls help me out?
Thanks
 
N

Neel

If you just want the low order bits;

unsigned char a = short_value;

If I do something like...

unsigned char x= 0xAA;
if(x==(unsigned char) 0xAA)

it returns false...

whats wrong here?
 
I

Ian Collins

*Last warning for quoting signatures!*
If I do something like...

unsigned char x= 0xAA;
if(x==(unsigned char) 0xAA)

it returns false...

whats wrong here?

Everything?

Why the cast?

You obviously didn't post what you tested.
 
N

Neel

Do it this way:

     msg = 0xAB;


I was just wondering, Char occupies 1 Byte and Short occupies 2
Bytes...

so in short 0xAB means lower byte is AB and higher byte is 00.
so when I assign, what byte would be assigned ???
 
P

Peter Nilsson

Ian Collins said:
If you just want the low order bits;

unsigned char a = short_value;

This needn't give you the same low order
value bits as the short if short_value
is negative.
 
P

Peter Nilsson

Same way you store a B-Double in the back of a coupe.

Since msg is unsigned char it makes no sense to cast
the value to char beforehand. What is the purpose of
the shift? [What are _really_ trying to do? It looks
like you're posting a broken solution to an undisclosed
problem.]
I was just wondering, Char occupies 1 Byte

ITYM char, but by definition, yes.
and Short occupies 2 Bytes...

A short may occupy 1 byte, or 4, or... It must have
at least 15 value bits and 1 sign bit (or 16 value
bits in the case of unsigned short), but how many
bytes those bits occupy is implementation dependant.

On many embedded implementations, short is 1 byte.
so in short 0xAB means lower byte is AB and higher
byte is 00.

No, 0xAB 'in short' means the short has the value 171.
If you want to know what the high and low octets of
a 16-bit representation are, then you should simply
mask and shift the value appropriately. You should
also use an unsigned short (or integer in general)
if you're going to do this.
so when I assign, what byte would be assigned

Since unsigned char is clearly unsigned, the value
stored in it will (by definition) be the value
of the expression modulo one more than UCHAR_MAX.
 
K

Keith Thompson

Neel said:
I was just wondering, Char occupies 1 Byte and Short occupies 2
Bytes...

Since C is case-sensitive, it's best not to capitalize type names;
write "char", not "Char".

char occupies 1 byte (by definition -- but a byte in C can be more
than 8 bits).

short occupies 2 bytes in your implementation, but this can vary. The
standard guarantees only that it's at least 16 bits (it actually
guarantees a range of at least -32767 to +32767, but that implies at
least 16 bits). It can be more than 2 bytes on some systems, and it
can be a single byte if a byte is at least 16 bits.
so in short 0xAB means lower byte is AB and higher byte is 00.
so when I assign, what byte would be assigned ???

When you assign something to an integer object, the *value* is
assigned. 0xAB is just another way of writing 171 -- and it's of type
int, not short. If you write:

unsigned char msg;
msg = 0xAB;

it just assigns the value 0xAB (equivalently, 171) to msg. The value
is implicitly converted from int to unsigned char. Since the range of
unsigned char is at least 0..255, the conversion is straightforward.

You're usually better off thinking in terms of values rather than
representations. And most casts (explicit conversions) are
unnecessary.
 
K

Keith Thompson

Peter Nilsson said:
This needn't give you the same low order
value bits as the short if short_value
is negative.

It does if short is 2's-complement with no padding bits.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,539
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top