why use unsigned char ever.


Ad

Advertisements

K

Kevin Goodsell

dave said:
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
 
A

Attila Feher

dave said:
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
 
R

Rolf Magnus

Attila said:
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.
 
A

Attila Feher

Rolf said:
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
 
Ad

Advertisements

J

Josephine Schafer

Rolf Magnus said:
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.
 
T

Thomas Matthews

dave said:
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
 
D

Default User

dave said:
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
 
Ad

Advertisements


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

Top