std :: cout将uint8_t作为字符处理 [英] std::cout deal with uint8_t as a character
问题描述
如果我运行以下代码:
std::cout << static_cast<uint8_t>(65);
它将输出:
A
与ASCII等效的数字为65.
这是因为uint8_t
被简单地定义为:
Which is the ASCII equivalent of the number 65.
This is because uint8_t
is simply defined as:
typedef unsigned char uint8_t;
-
此行为是标准行为吗?
Is this behavior a standard?
不是最好的方法来定义保证以数字而不是字符处理的
uint8_t
吗?Should not be a better way to define
uint8_t
that guaranteed to be dealt with as a number not a character?我无法理解如果要打印
uint8_t
变量的值将被打印为字符的逻辑.I can not understand the logic that if I want to print the value of a
uint8_t
variable, it will be printed as a character.P.S.我正在使用MSVS 2013.
P.S. I am using MSVS 2013.
推荐答案
此行为是否是标准行为
Is this behavior a standard
此行为是标准行为,因为如果
uint8_t
是unsigned char
的typedef,则它将始终打印字符,因为std::ostream
具有unsigned char
的重载,并将变量的内容作为字符打印出来.The behavior is standard in that if
uint8_t
is a typedef ofunsigned char
then it will always print a character asstd::ostream
has an overload forunsigned char
and prints out the contents of the variable as a character.不是最好的方法来定义
uint8_t
来保证将其处理为数字而不是字符吗?Should not be a better way to define
uint8_t
that guaranteed to be dealt with as a number not a character?为此,C ++委员会将不得不引入一个新的基本类型.当前,唯一具有
sizeof()
等于1的类型是char
,signed char
和unsigned char
.可能他们可以使用bool
,但是bool
的大小不必为1,则您仍然在同一条船上,因为In order to do this the C++ committee would have had to introduce a new fundamental type. Currently the only types that has a
sizeof()
that is equal to 1 ischar
,signed char
, andunsigned char
. It is possible they could use abool
butbool
does not have to have a size of 1 and then you are still in the same boat sinceint main() { bool foo = 42; std::cout << foo << '\n'; }
将打印
1
,而不是42
,因为任何非零都为true,并且true打印为1
,但为默认值.will print
1
, not42
as any non zero is true and true is printed as1
but default.I'm not saying it can't be done but it is a lot of work for something that can be handled with a cast or a function
C ++ 17引入了
std::byte
,其定义为enum class byte : unsigned char {};
.因此它将是一个字节宽,但不是字符类型.不幸的是,由于它是enum class
,所以它有其自身的局限性.已经为它定义了按位运算符,但是没有内置的流运算符,因此您需要定义自己的输入和输出.这意味着您仍在转换它,但至少您不会与unsigned char
的内置运算符发生冲突.这给你类似的东西C++17 introduces
std::byte
which is defined asenum class byte : unsigned char {};
. So it will be one byte wide but it is not a character type. Unfortunately, since it is anenum class
it comes with it's own limitations. The bit-wise operators have been defined for it but there is no built in stream operators for it so you would need to define your own to input and output it. That means you are still converting it but at least you wont conflict with the built in operators forunsigned char
. That gives you something likestd::ostream& operator <<(std::ostream& os, std::byte b) { return os << std::to_integer<unsigned int>(b); } std::istream& operator <<(std::istream& is, std::byte& b) { unsigned int temp; is >> temp; b = std::byte{b}; return is; } int main() { std::byte foo{10}; std::cout << foo; }
这篇关于std :: cout将uint8_t作为字符处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!