C++ 中有“字节”数据类型吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20024690/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there 'byte' data type in C++?
提问by user2972135
If exists is there header file to include?
如果存在,是否有要包含的头文件?
This code give compilation error:
此代码给出编译错误:
#include <iostream>
using namespace std;
int main()
{
byte b = 2;
cout << b << endl;
return 0;
}
采纳答案by Darren Gansberg
No there is no byte data type in C++. However you could always include the bitset header from the standard library and create a typedef for byte:
不,C++ 中没有字节数据类型。但是,您始终可以包含标准库中的 bitset 标头并为字节创建一个 typedef:
typedef bitset<8> BYTE;
NB: Given that WinDef.h defines BYTE for windows code, you may want to use something other than BYTE if your intending to target Windows.
注意:鉴于 WinDef.h 为 Windows 代码定义了 BYTE,如果您打算以 Windows 为目标,您可能需要使用 BYTE 以外的其他内容。
Edit: In response to the suggestion that the answer is wrong. The answer is not wrong. The question was "Is there a 'byte' data type in C++?". The answer was and is: "No there is no byte data type in C++" as answered.
编辑:为了回应答案错误的建议。答案没有错。问题是“C++ 中是否有‘字节’数据类型?”。答案是并且是:“不,C++ 中没有字节数据类型”作为回答。
With regards to the suggested possible alternative for which it was asked why is the suggested alternative better?
关于建议的可能替代方案,有人问为什么建议的替代方案更好?
According to my copy of the C++ standard, at the time:
根据我当时的 C++ 标准副本:
"Objects declared as characters (char) shall be large enough to store any member of the implementations basic character set": 3.9.1.1
“声明为字符(char)的对象应足够大以存储实现基本字符集的任何成员”:3.9.1.1
I read that to suggest that if a compiler implementation requires 16 bits to store a member of the basic character set then the size of a char would be 16 bits. That today's compilers tend to use 8 bits for a char is one thing, but as far as I can tell there is certainly no guarantee that it will be 8 bits.
我读到它建议如果编译器实现需要 16 位来存储基本字符集的成员,那么 char 的大小将为 16 位。今天的编译器倾向于将 8 位用于 char 是一回事,但据我所知,肯定不能保证它是 8 位。
On the other hand, "the class template bitset<N> describes an object that can store a sequence consisting of a fixed number of bits, N." : 20.5.1. In otherwords by specifying 8 as the template parameter I end up with an object that can store a sequence consisting of 8 bits.
另一方面,“类模板 bitset<N> 描述了一个可以存储由固定位数 N 组成的序列的对象。” :20.5.1。换句话说,通过指定 8 作为模板参数,我最终得到一个可以存储由 8 位组成的序列的对象。
Whether or not the alternative is better to char, in the context of the program being written, therefore depends, as far as I understand, although I may be wrong, upon your compiler and your requirements at the time. It was therefore upto the individual writing the code, as far as I'm concerned, to do determine whether the suggested alternative was appropriate for their requirements/wants/needs.
因此,在正在编写的程序的上下文中,替代方案是否比 char 更好取决于您的编译器和您当时的要求,据我所知,尽管我可能是错的。因此,就我而言,由编写代码的个人来确定建议的替代方案是否适合他们的要求/想要/需要。
回答by jwodder
No, there is no type called "byte
" in C++. What you want instead is unsigned char
(or, if you need exactly 8 bits, uint8_t
from <cstdint>
, since C++11). Note that char
is not necessarily an accurate alternative, as it means signed char
on some compilers and unsigned char
on others.
不,byte
C++ 中没有名为“ ”的类型。你想要的是unsigned char
(或者,如果你正好需要 8 位,uint8_t
来自<cstdint>
,因为 C++11)。请注意,这char
不一定是准确的替代方案,因为它意味着signed char
在某些编译器和unsigned char
其他编译器上。
回答by maxschlepzig
回答by rmp
if you are using windows, in WinDef.h you have:
如果您使用的是 Windows,则在 WinDef.h 中您有:
typedef unsigned char BYTE;
回答by m8mble
Using C++11
there is a nice version for a manually defined byte type:
使用C++11
手动定义的字节类型有一个很好的版本:
enum class byte : std::uint8_t {};
That's at least what the GSLdoes.
这至少是GSL所做的。
Starting with C++17
(almost) this very version is defined in the standard as std::byte
(thanks Neil Macintosh for both).
从C++17
(几乎)开始,这个版本在标准中被定义为std::byte
(感谢 Neil Macintosh)。