storing short in char

  • Follow


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
0
Reply a.k.vora (41) 10/15/2008 10:39:16 PM

Neel wrote:
> Hi friends,
> How can I store a short into an unsigned char???
> 
If you just want the low order bits;

unsigned char a = short_value;

-- 
Ian Collins
0
Reply ian-news (9880) 10/15/2008 10:45:22 PM


On Oct 15, 3:45=A0pm, Ian Collins <ian-n...@hotmail.com> wrote:
> Neel wrote:
> > Hi friends,
> > How can I store a short into an unsigned char???
>
> If you just want the low order bits;
>
> unsigned char a =3D short_value;
>
> --
> Ian Collins

If I do something like...

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

it returns false...

whats wrong here?
0
Reply a.k.vora (41) 10/15/2008 10:59:34 PM

Neel wrote:
> On Oct 15, 3:45 pm, Ian Collins <ian-n...@hotmail.com> wrote:
>> Neel wrote:
>>> Hi friends,
>>> How can I store a short into an unsigned char???
>> If you just want the low order bits;
>>
>> unsigned char a = short_value;
>>
*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.

-- 
Ian Collins
0
Reply ian-news (9880) 10/15/2008 11:03:35 PM

Neel wrote:
> 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.

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

Do it this way:

     msg = 0xAB;

-- 
pete
0
Reply pfiland (6613) 10/15/2008 11:12:01 PM

On Oct 15, 4:12=A0pm, pete <pfil...@mindspring.com> wrote:
> Neel wrote:
> > 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.
> > unsigned char msg;
> > msg=3D(char)(0xAB<<8);
>
> Do it this way:
>
> =A0 =A0 =A0msg =3D 0xAB;
>
> --
> pete

thank you
0
Reply a.k.vora (41) 10/15/2008 11:19:17 PM

On Oct 15, 4:12=A0pm, pete <pfil...@mindspring.com> wrote:
> Neel wrote:
> > 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.
> > unsigned char msg;
> > msg=3D(char)(0xAB<<8);
>
> Do it this way:
>
> =A0 =A0 =A0msg =3D 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 ???



0
Reply a.k.vora (41) 10/15/2008 11:20:53 PM

Ian Collins <ian-n...@hotmail.com> wrote:
> Neel wrote:
> > Hi friends,
> > How can I store a short into an
> > unsigned char???
>
> 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.

--
Peter
0
Reply airia (1802) 10/16/2008 12:26:04 AM

Neel <a.k.v...@gmail.com> wrote:
> pete <pfil...@mindspring.com> wrote:
> > Neel wrote:
> > > How can I store a short into an unsigned char???

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

> > > its like
> > >
> > > unsigned char msg;
> > >
> > > //now I want to assign 0xAB into it.
> > > unsigned char msg;
> > > msg=3D(char)(0xAB<<8);

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.]

> > Do it this way:
> >
> > =A0 =A0 =A0msg =3D 0xAB;
>
> 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.

--
Peter
0
Reply airia (1802) 10/16/2008 12:35:24 AM

Neel <a.k.vora@gmail.com> writes:
> On Oct 15, 4:12�pm, pete <pfil...@mindspring.com> wrote:
>> Neel wrote:
>> > 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.
>> > unsigned char msg;
>> > msg=(char)(0xAB<<8);
>>
>> Do it this way:
>>
>> � � �msg = 0xAB;
>>
>
>
> 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.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Nokia
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
0
Reply kst-u (21469) 10/16/2008 12:40:01 AM

Peter Nilsson <airia@acay.com.au> writes:
> Ian Collins <ian-n...@hotmail.com> wrote:
>> Neel wrote:
>> > Hi friends,
>> > How can I store a short into an
>> > unsigned char???
>>
>> 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.

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

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Nokia
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
0
Reply kst-u (21469) 10/16/2008 12:46:21 AM

Keith Thompson <ks...@mib.org> wrote:
> Peter Nilsson <ai...@acay.com.au> writes:
> > Ian Collins <ian-n...@hotmail.com> wrote:
> > > 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.
>
> It does if short is 2's-complement with
> no padding bits.

Even with padding bits, that would be the case.

--
Peter
0
Reply airia (1802) 10/16/2008 12:48:34 AM

11 Replies
32 Views

(page loaded in 0.164 seconds)

Similiar Articles:























6/23/2012 3:39:10 PM


Reply: