std::cout outputs unsigned char type data

When writing C++ code, you may find that it is normal to output types such as char, float, int, and double, but sometimes garbled characters are output when encountering unsigned char.
So test it yourself:

int main(int argc, char** argv)
{
    
    
	std::cout << "test for uchar" << std::endl;
	int h = 255;
	//unsigned char u = (unsigned char)(h);

	unsigned char u = '255';
	char c = 'a';
	float f = 1.0f;
	double d = 1.0f;
	int i = 1;

	std::cout << "unsigned char: " << u << std::endl;
	std::cout << "char: " << c << std::endl;
	std::cout << "float: " << f << std::endl;
	std::cout << "double: " << d << std::endl;
	std::cout << "int: " << i << std::endl;
}

The output is normal:
insert image description here

Unsigned char is generally between 0-255, when we give a value in the way of assignment:

int main(int argc, char** argv)
{
    
    
	std::cout << "test for uchar" << std::endl;
	int h = 255;
	unsigned char u = (unsigned char)(h);

	//unsigned char u = '255';
	char c = 'a';
	float f = 1.0f;
	double d = 1.0f;
	int i = 1;

	std::cout << "unsigned char: " << u << std::endl;
	std::cout << "char: " << c << std::endl;
	std::cout << "float: " << f << std::endl;
	std::cout << "double: " << d << std::endl;
	std::cout << "int: " << i << std::endl;
}

The resulting output is garbage value:
insert image description here
It will not work because here u(unsigned char) is a unsigned char (with value 255), 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 255 is non-printable. Check out here for the ASCII table.
PS You can check whether u is printable or not using isprint() as
above means: when given this exceeds The ASCII range is out, so it cannot be printed, or it is a garbage value.

Final test:
When u is given a normal normal value, 97 corresponds to 'a', and the output is 'a'. The test results are as follows:
Test code:

int main(int argc, char** argv)
{
    
    
	std::cout << "test for uchar" << std::endl;
	int h = 97;
	unsigned char u = (unsigned char)(h);

	//unsigned char u = '255';
	char c = 'a';
	float f = 1.0f;
	double d = 1.0f;
	int i = 1;

	std::cout << "unsigned char: " << u << std::endl;
	std::cout << "char: " << c << std::endl;
	std::cout << "float: " << f << std::endl;
	std::cout << "double: " << d << std::endl;
	std::cout << "int: " << i << std::endl;
}

Test results
insert image description here
Therefore, when std::cout cannot come out, you can use forced conversion to int, and cout is fine.
code show as below:

int main(int argc, char** argv)
{
    
    
	std::cout << "test for uchar" << std::endl;
	int h = 255;
	unsigned char u = (unsigned char)(h);

	//unsigned char u = '255';
	char c = 'a';
	float f = 1.0f;
	double d = 1.0f;
	int i = 1;

	std::cout << "unsigned char: " << (int)u << std::endl;
	std::cout << "char: " << c << std::endl;
	std::cout << "float: " << f << std::endl;
	std::cout << "double: " << d << std::endl;
	std::cout << "int: " << i << std::endl;
}

Test results:
insert image description here
see the following materials:
https://www.it1352.com/457126.html

Guess you like

Origin blog.csdn.net/weixin_43851636/article/details/110552887