why use unsigned char ever.

Discussion in 'C++' started by dave, Aug 15, 2003.

  1. dave

    dave Guest

    Why do we ever need to use unsigned char? Any examples will help a lot.
    dave, Aug 15, 2003
    #1
    1. Advertising

  2. dave wrote:
    > Why do we ever need to use unsigned char? Any examples will help a lot.


    Example: You need a 1 byte unsigned value.

    Maybe you need to explain your question a little better.

    -Kevin
    --
    My email address is valid, but changes periodically.
    To contact me please use the address from a recent posting.
    Kevin Goodsell, Aug 15, 2003
    #2
    1. Advertising

  3. dave

    Attila Feher Guest

    dave wrote:
    > Why do we ever need to use unsigned char? Any examples will help a
    > lot.


    Any time you need a so-called byte (do not mix it with octet, bytes can have
    more than 8 bits). For example in preparing a bit-map command for a device,
    which is driven by command bytes. Also buffers (usually command buffers)
    for external devices or (binary) protocol are built using unsigned
    characters.

    Also please remember, that it is implementation defined if char (without
    telling if it is signed or not) is signed or not! So on some architectures
    the char used is in fact an unsigned char.

    Attila aka WW
    Attila Feher, Aug 15, 2003
    #3
  4. dave

    Rolf Magnus Guest

    Attila Feher wrote:

    > Also please remember, that it is implementation defined if char
    > (without telling if it is signed or not) is signed or not! So on some
    > architectures the char used is in fact an unsigned char.


    Not exactly. char and unsigned char are always distinct types, even on
    systems where a char behaves the same as an unsigned char.
    Rolf Magnus, Aug 15, 2003
    #4
  5. dave

    Attila Feher Guest

    Rolf Magnus wrote:
    > Attila Feher wrote:
    >
    >> Also please remember, that it is implementation defined if char
    >> (without telling if it is signed or not) is signed or not! So on
    >> some architectures the char used is in fact an unsigned char.

    >
    > Not exactly. char and unsigned char are always distinct types, even on
    > systems where a char behaves the same as an unsigned char.


    Yes, they are distinct. That is why I did not say that its type is unsigned
    char. :)

    A aka WW
    Attila Feher, Aug 15, 2003
    #5
  6. On 14 Aug 2003 23:47:30 -0700, (dave) wrote:

    >Why do we ever need to use unsigned char? Any examples will help a lot.


    One example is practical usage of the standard toupper function.
    Alf P. Steinbach, Aug 15, 2003
    #6
  7. "Rolf Magnus" <> wrote in message
    news:bhi7he$9hr$05$-online.com...
    > Attila Feher wrote:
    >
    > > Also please remember, that it is implementation defined if char
    > > (without telling if it is signed or not) is signed or not! So on some
    > > architectures the char used is in fact an unsigned char.

    >
    > Not exactly. char and unsigned char are always distinct types, even on
    > systems where a char behaves the same as an unsigned char.


    Just to add that char, signed char and unsigned char are three distinct
    types.

    --
    JS
    Josephine Schafer, Aug 15, 2003
    #7
  8. dave wrote:

    > Why do we ever need to use unsigned char? Any examples will help a lot.


    I use unsigned char when I need to access the smallest size unit,
    with the full range of the unit.

    On many platforms that I work on, CHAR_BITS (number of bits in a char),
    is 8. The data I need to manipulate is in these units and has the
    range of 0..255. I can read in a whole buffer then parse out
    the data.

    --
    Thomas Matthews

    C++ newsgroup welcome message:
    http://www.slack.net/~shiva/welcome.txt
    C++ Faq: http://www.parashift.com/c -faq-lite
    C Faq: http://www.eskimo.com/~scs/c-faq/top.html
    alt.comp.lang.learn.c-c++ faq:
    http://www.raos.demon.uk/acllc-c /faq.html
    Other sites:
    http://www.josuttis.com -- C++ STL Library book
    Thomas Matthews, Aug 15, 2003
    #8
  9. dave

    Default User Guest

    dave wrote:
    >
    > Why do we ever need to use unsigned char? Any examples will help a lot.


    Unsigned char is guaranteed never to have trap values, and its values
    are pure binary notation. You access any object as an array of unsigned
    char to view its value representation.


    Brian Rodenborn
    Default User, Aug 15, 2003
    #9
  10. Thomas Matthews wrote:

    >
    > On many platforms that I work on, CHAR_BITS (number of bits in a char),
    > is 8.


    That should be CHAR_BIT without an S.

    -Kevin
    --
    My email address is valid, but changes periodically.
    To contact me please use the address from a recent posting.
    Kevin Goodsell, Aug 16, 2003
    #10
    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. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,849
    Smokey Grindel
    Dec 2, 2006
  2. Steffen Fiksdal

    void*, char*, unsigned char*, signed char*

    Steffen Fiksdal, May 8, 2005, in forum: C Programming
    Replies:
    1
    Views:
    582
    Jack Klein
    May 9, 2005
  3. Ioannis Vranos
    Replies:
    11
    Views:
    758
    Ioannis Vranos
    Mar 28, 2008
  4. Alex Vinokur
    Replies:
    9
    Views:
    791
    James Kanze
    Oct 13, 2008
  5. Replies:
    11
    Views:
    733
Loading...

Share This Page