storing short in char

Discussion in 'C Programming' started by Neel, Oct 15, 2008.

  1. Neel

    Neel Guest

    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
     
    Neel, Oct 15, 2008
    #1
    1. Advertising

  2. Neel

    Ian Collins Guest

    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
     
    Ian Collins, Oct 15, 2008
    #2
    1. Advertising

  3. Neel

    Neel Guest

    On Oct 15, 3:45 pm, Ian Collins <> 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;
    >
    > --
    > Ian Collins


    If I do something like...

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

    it returns false...

    whats wrong here?
     
    Neel, Oct 15, 2008
    #3
  4. Neel

    Ian Collins Guest

    Neel wrote:
    > On Oct 15, 3:45 pm, Ian Collins <> 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
     
    Ian Collins, Oct 16, 2008
    #4
  5. Neel

    Neel Guest

    On Oct 15, 4:12 pm, pete <> 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;
    >
    > --
    > pete


    thank you
     
    Neel, Oct 16, 2008
    #5
  6. Neel

    Neel Guest

    On Oct 15, 4:12 pm, pete <> 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...

    so in short 0xAB means lower byte is AB and higher byte is 00.
    so when I assign, what byte would be assigned ???
     
    Neel, Oct 16, 2008
    #6
  7. Ian Collins <> 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
     
    Peter Nilsson, Oct 16, 2008
    #7
  8. Neel <> wrote:
    > pete <> 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=(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:
    > >
    > >      msg = 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
     
    Peter Nilsson, Oct 16, 2008
    #8
  9. Neel <> writes:
    > On Oct 15, 4:12 pm, pete <> 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) <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"
     
    Keith Thompson, Oct 16, 2008
    #9
  10. Peter Nilsson <> writes:
    > Ian Collins <> 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) <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"
     
    Keith Thompson, Oct 16, 2008
    #10
  11. Keith Thompson <> wrote:
    > Peter Nilsson <> writes:
    > > Ian Collins <> 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
     
    Peter Nilsson, Oct 16, 2008
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. SinusX

    char + short = char*

    SinusX, May 18, 2004, in forum: C++
    Replies:
    5
    Views:
    428
    Sharad Kala
    May 18, 2004
  2. lovecreatesbeauty
    Replies:
    1
    Views:
    1,151
    Ian Collins
    May 9, 2006
  3. David Geering

    longs, long longs, short short long ints . . . huh?!

    David Geering, Jan 8, 2007, in forum: C Programming
    Replies:
    15
    Views:
    592
    Keith Thompson
    Jan 11, 2007
  4. Replies:
    4
    Views:
    866
    Kaz Kylheku
    Oct 17, 2006
  5. Ioannis Vranos

    unsigned short, short literals

    Ioannis Vranos, Mar 4, 2008, in forum: C Programming
    Replies:
    5
    Views:
    731
    Eric Sosman
    Mar 5, 2008
Loading...

Share This Page