Cout Not Printing Unsigned Char

Why cout works weird for unsigned char?

As you have commented, the reason is that you use cout to print its content directly. Here I will try to explain to you why this will not work.

cout << bottomRGB[0] << endl;

Why "cout" works weird for "unsigned char"?

It will not work because here bottomRGB[0] is a unsigned char (with value 218), cout actually will print some garbage value (or nothing) as it is just a non-printable ASCII character which is getting printed anyway. Note that ASCII character corresponding to 218 is non-printable. Check out here for the ASCII table.

P.S. You can check whether bottomRGB[0] is printable or not using isprint() as:

cout << isprint(bottomRGB[0]) << endl; // will print garbage value or nothing

It will print 0 (or false) indicating the character is non-printable


For your example, to make it work, you need to type cast it first before cout:

cout << (int) bottomRGB[0] << endl; // correctly printed (218 for your example) 

uint8_t can't be printed with cout

It doesn't really print a blank, but most probably the ASCII character with value 5, which is non-printable (or invisible). There's a number of invisible ASCII character codes, most of them below value 32, which is the blank actually.

You have to convert aa to unsigned int to output the numeric value, since ostream& operator<<(ostream&, unsigned char) tries to output the visible character value.

uint8_t aa=5;

cout << "value is " << unsigned(aa) << endl;

Cout unsigned char

The "sane" answer is: don't rely on extended-ASCII characters. Unicode is widespread enough to make this the preferred approach:

#include <iostream>
int main() {
std::cout << u8"\u00e0\n";
}

This will explicitly print the character à you requested; in fact, that's also how your browser understands it, which you can easily verify by putting into e.g. some unicode character search, which will result in LATIN SMALL LETTER A WITH GRAVE, with the code U+00E0 which you can spot in the code above.

In your example, there's no difference between using a signed or unsigned char; the byte value 133 gets written to the terminal, but the way it interprets it might differ from machine to machine, basing on how it's actually set up to interpret it. In fact, in a UTF-8 console, this is simply a wrong unicode sequence (u"\0x85" isn't a valid character) - if your OS was switched to UTF-8, that might be why you're seeing no output.

cout derefernced (unsigned char) pointers, gives me unexpected results

'8' is the character 8.
8 is just a code of a character which is invisible.

Try writing *buffer = 48; and guess why the output is "Buffer = 0" based on this table.

std::cout not properly printing std::string created by reinterpret_cast of unsigned char array

Change

std::cout << my_std_string << std::endl;    // Bad stuff happens :S

to

for( std::size_t i = 0; i < my_txt_len ; i++ )
{
std::cout << std::hex << static_cast<unsigned>(my_std_string[i]) << " " ;
}
std::cout << std::endl;

A std::string is a representation of a string, not purely an array of bytes. Therefore, passing it to std::cout will show a string. Your printf is printing individual values of your unsigned char array. The stl equivalent of this is an std::vector<unsigned char>.

You need to add the static_cast<unsigned>(). Otherwise std::cout will print each unsigned char value as a char ascii character. The output will be R _ s h ~ ) 3 t t s r U. You have to prevent this conversion by implicitly telling it.



Related Topics



Leave a reply



Submit