简体   繁体   English

从 `int` 转换为 `unsigned char`

[英]Casting from `int` to `unsigned char`

I am running the following C++ code on Coliru :我在Coliru上运行以下 C++ 代码:

#include <iostream>
#include <string>

int main()
{
     int num1 = 208;
     unsigned char uc_num1 = (unsigned char) num1;
     std::cout << "test1: " << uc_num1 << "\n";

     int num2 = 255;
     unsigned char uc_num2 = (unsigned char) num2;
     std::cout << "test2: " << uc_num2 << "\n";
}

I am getting the output:我得到输出:

test1: �

test2: �

This is a simplified example of my code.这是我的代码的简化示例。

Why does this not print out:为什么不打印出来:

test1: 208

test2: 255

Am I misusing std::cout , or am I not doing the casting correctly?我是在滥用std::cout ,还是我没有正确地进行转换?


More background更多背景

I want to convert from int to unsigned char (rather than unsigned char* ).我想从int转换为unsigned char (而不是unsigned char* )。 I know that all my integers will be between 0 and 255 because I am using them in the RGBA color model.我知道我所有的整数都在 0 到 255 之间,因为我在 RGBA 颜色模型中使用它们。

I want to use LodePNG to encode images.我想使用LodePNG对图像进行编码。 The library in example_encode.cpp uses unsigned char s in std::vector<unsigned char>& image : example_encode.cpp的库在std::vector<unsigned char>& image使用unsigned char s:

//Example 1
//Encode from raw pixels to disk with a single function call
//The image argument has width * height RGBA pixels or width * height * 4 bytes
void encodeOneStep(const char* filename, std::vector<unsigned char>& image, unsigned width, unsigned height)
{
  //Encode the image
  unsigned error = lodepng::encode(filename, image, width, height);

  //if there's an error, display it
  if(error) std::cout << "encoder error " << error << ": "<< lodepng_error_text(error) << std::endl;
}

std::cout is correct =) std::cout 是正确的 =)

Press ALT then 2 0 8 This is the char that you are printing with test1.按 ALT 然后按 2 0 8 这是您使用 test1 打印的字符。 The console might not know how to print that properly so it outputs the question mark.控制台可能不知道如何正确打印,因此它输出问号。 Same thing with 255. After reading the png and putting it in the std::vector, there is no use of writing it to the screen. 255也是一样。读取png并将其放入std :: vector后,将其写入屏幕是没有用的。 This file contains binary data which is not writable.该文件包含不可写的二进制数据。

If you want to see "208" and "255", you should not convert them to unsigned char first, or specify that you want to print numbers such as int for example, like this如果你想看到“208”和“255”,你不应该先把它们转换成无符号字符,或者指定你要打印诸如int之类的数字,像这样

std::cout << num1 << std::endl;
std::cout << (int) uc_num1 << std::endl;

You are looking at a special case of std::cout which is not easy to understand at first.您正在查看 std::cout 的一个特例,它起初并不容易理解。

When std::cout is called, it checks the type of the right hand side operand.当 std::cout 被调用时,它会检查右侧操作数的类型。 In your case, std::cout << uc_num1 tells cout that the operand is an unsigned char, so it does not perform a conversion because unsigned char are usually printable.在您的情况下, std::cout << uc_num1告诉 cout 操作数是无符号字符,因此它不会执行转换,因为无符号字符通常是可打印的。 Try this :尝试这个 :

unsigned char uc_num3 = 65;
std::cout << uc_num3 << std::endl;

If you write std::cout << num1 , then cout will realize that you are printing an int.如果您编写std::cout << num1 ,则 cout 会意识到您正在打印一个 int。 It will then transform the int into a string and print that string for you.然后它将 int 转换为字符串并为您打印该字符串。

You might want to check about c++ operator overloading to understand how it works, but it is not super crucial at the moment, you just need to realize that std::cout can behave differently for different data type you try to print.您可能想检查 C++ 运算符重载以了解它是如何工作的,但目前这不是非常重要,您只需要意识到 std::cout 对于您尝试打​​印的不同数据类型可能会有不同的表现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM