In C/C++, what an unsigned char is used for? How is it different from a regular char?
Join them; it only takes a minute:
|
|
|
In C++, there are three distinct character types:
If you are using character types for text, use the unqualified
It also works out as a number value, but it is unspecified whether that value is treated as signed or unsigned. Beware character comparisons through inequalities - although if you limit yourself to ASCII (0-127) you're just about safe. If you are using character types as numbers, use:
"At least", because the C++ standard only gives the minimum range of values that each numeric type is required to cover. |
|||||||||||||||||||||
|
|
This is implementation dependent, as the C standard does NOT define the signed-ness of The difference between There is a nice summary of this issue here. As others have mentioned since I posted this, you're better off using |
|||||||||
|
|
Because i feel it's really called for, i just want to state some rules of C and C++ (they are the same in this regard). First, all bits of Now, i had a discussion with someone about what happens when you convert the value
That's a mathematical description. C++ describes it in terms of modulo calculus, which yields to the same rule. Anyway, what is not guaranteed is that all bits in the integer
That's enough, actually! So whenever you want to have an
It also follows that a conversion is not just truncating higher order bits. The fortunate event for two's complement is that it is just a truncation there, but the same isn't necessarily true for other sign representations. |
|||||||||||||||||
|
|
As for example usages of unsigned char: unsigend char is often used in computer graphics which very often (though not always) assigns a single byte to each colour component. It is common to see an RGB (or RGBA) colour represented as 24 (or 32) bits, each an unsigned char. Since unsigned char values fall in the range [0,255], the values are typically interpreted as
So you would end up with RGB red as (255,0,0) -> (100% red, 0% green, 0% blue). Why not use a signed char? Arithmetic and bit shifting becomes problematic. As explained already, a signed char's range is essentially shifted by -128. A very simple and naive (mostly unused) method for converting RGB to grayscale is to average all three colour components, but this runs into problems when the values of the colour components are negative. Red (255, 0, 0) averages to (85, 85, 85) when using unsigned char arithmetic. However, if the values were signed chars (127,-128,-128), we would end up with (-99, -99, -99), which would be (29, 29, 29) in our unsigned char space, which is incorrect. |
|||
|
|
|
If you want to use a character as a small integer, the safest way to do it is with the |
|||||
|
|
|
|||||
|
|
An unsigned char is a (unsigned) byte value (0 to 255). You may be thinking of "char" in terms of being a "character" but it is really a numerical value. The regular "char" is signed, so you have 128 values, and these values map to characters using ASCII encoding. But in either case, what you are storing in memory is a byte value. |
|||
|
|
|
In terms of direct values a regular char is used when the values are known to be between In terms of what it's used for, the standards allow objects of POD (plain old data) to be directly converted to an array of unsigned char. This allows you to examine the representation and bit patterns of the object. The same guarantee of safe type punning doesn't exist for char or signed char. |
|||||||||||||||||||||
|
|
If you like using various types of specific length and signedness, you're probably better off with uint8_t, int8_t, uint16_t, etc simply because they do exactly what they say. |
|||
|
|
|
unsigned char is the heart of all bit trickery. In almost ALL compiler for ALL platform an unsigned char is simply a BYTE. An unsigned integer of (usually) 8 bits. that can be treated as a small integer or a pack of bits. In addiction, as someone else has said, the standard doesn't define the sign of a char. so you have 3 distinct "char" types: char, signed char, unsigned char. |
|||||||||
|
|
where as
|
||||
|
|
|
If you're using C-style strings, just use |
||||
|
|
|
Some googling found this, where people had a discussion about this. An unsigned char is basically a single byte. So, you would use this if you need one byte of data (for example, maybe you want to use it to set flags on and off to be passed to a function, as is often done in the Windows API). |
|||
|
|
|
An unsigned char uses the bit that is reserved for the sign of a regular char as another number. This changes the range to [0 - 255] as opposed to [-128 - 127]. Generally unsigned chars are used when you don't want a sign. This will make a difference when doing things like shifting bits (shift extends the sign) and other things when dealing with a char as a byte rather than using it as a number. |
|||
|
|
|
unsigned numbers are example: if chars are 8 bits, |
|||
|
|
protected by Bartek Banachewicz Oct 13 '15 at 8:27
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?