为什么"cout"对"unsigned char"很奇怪?
Why "cout" works weird for "unsigned char"?
我有以下代码:
cvtColor (image, image, CV_BGRA2RGB);
Vec3b bottomRGB;
bottomRGB=image.at<Vec3b>(821,1232);
当我显示 bottomRGB[0]
时,它显示一个大于255的值。
正如您所评论的,原因是您使用cout
直接打印其内容。在这里,我将尝试向您解释为什么这不起作用。
cout << bottomRGB[0] << endl;
为什么"cout"
为"unsigned char"
工作很奇怪?
它将不起作用,因为bottomRGB[0]
是unsigned char
(具有值218
),cout
实际上会打印一些垃圾值(或没有),因为它只是 non--无论如何都会打印出可打印的ASCII字符。请注意,与218
相对应的ASCII字符是不可打印的。在此处查看ASCII表。
P.S。您可以检查bottomRGB[0]
是否可以使用isprint()
为:
cout << isprint(bottomRGB[0]) << endl; // will print garbage value or nothing
它将打印0
(或false
),表明该字符是不可打印的
为了您的示例,要使它起作用,您需要在cout
之前先键入铸件:
cout << (int) bottomRGB[0] << endl; // correctly printed (218 for your example)
相关文章:
- 从"int*"强制转换为"unsigned int"会丢失精度错误
- 请解释这句话(cout<<1+int((a<b)^((b-a)&1) )<<endl
- 不能在初始值设定项列表中将非常量表达式从类型 'int' 缩小到'unsigned long long'
- std::cout.imbue()多重调用
- 如何在C++中用std::cout正确显示带十六进制的字符串文本
- 为什么在C的循环中使用printf的Rust代码不显示输出,而在C++的循环中显示std::cout
- 为什么我应该在异常处理中使用std::cerr而不是std::cout
- <Windows>为什么 std::thread::native_handle 返回类型为"long long unsigned int"的值,而不是 void*(又名 HANDLE)?
- 在作为静态成员包含在另一个类中的类的构造函数中使用 cout
- 在 COUT 语句中使用 COUT 调用函数
- GCC 4.8.2 自动矢量化由于 cout 而失败
- std::cout输出int时出现编译错误
- 在这种情况下显式调用时,std::cout 如何更改析构函数的行为?
- C++ Setter/Getter,cout 工作,printf 失败
- C++ cout 将双精度对齐到精度 2 并正确对齐
- 结构元素名称要 cout?
- 使用模板类重载 cout
- 为什么"cout"对"unsigned char"很奇怪?
- 我如何传递一个布尔数组或Unsigned Char*到cout
- Std::cout不能正确打印由unsigned char数组的reinterpret_cast创建的Std::stri