将 char[] 数组转换为 byte[],反之亦然?C++

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/876213/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 17:44:55  来源:igfitidea点击:

Convert array of char[] to byte[] and vice versa? C++

c++char

提问by

What is the best way to convert an array of chars to bytes and vice versa?

将字符数组转换为字节的最佳方法是什么,反之亦然?

Solution:

解决方案:

void CharToByte(char* chars, byte* bytes, unsigned int count){
    for(unsigned int i = 0; i < count; i++)
        bytes[i] = (byte)chars[i];
}

void ByteToChar(byte* bytes, char* chars, unsigned int count){
    for(unsigned int i = 0; i < count; i++)
         chars[i] = (char)bytes[i];
}

回答by JaredPar

The type char is one of the few types that has a size guaranteed by the ANSI standard and that size is 1 byte. As far as I know C does not directly define the type byte. However it would be just short of insane to have a type named byte which is not in fact a byte in size. Therefore a simple cast should do the trick.

类型 char 是少数具有 ANSI 标准保证的大小并且该大小为 1 个字节的类型之一。据我所知,C 并没有直接定义类型字节。然而,拥有一个名为 byte 的类型实际上并不是一个字节的大小,这简直是疯了。因此,简单的演员表应该可以解决问题。

回答by ?zgür

There is no byte type in C++, and according to the Standard:

C++ 中没有字节类型,根据标准:

Edit:

编辑:

1.7:

1.7:

A byte is at least large enough to contain any member of the basic execution character set and is composed of a contiguous sequence of bits, the number of which is implementation-defined.

一个字节至少足够大以包含基本执行字符集的任何成员,并且由连续的位序列组成,位的数量由实现定义。

5.3.3:

5.3.3:

sizeof(char), sizeof(signed char) and sizeof(unsigned char) are 1; the result of sizeof applied to any other fundamental type (3.9.1) is implementation-defined.

sizeof(char)、sizeof(signed char) 和 sizeof(unsigned char) 为 1;应用于任何其他基本类型 (3.9.1) 的 sizeof 结果是实现定义的。

回答by Demi

There is no byte type in C++. You could typedef 'unsigned char' to 'byte' if that makes it nicer. Really thats all a byte is in C++ - an unsigned char. Aside from that, yes I would cast... but this cast is better:

C++ 中没有字节类型。如果这样更好,您可以将 'unsigned char' 类型定义为 'byte'。真的这就是 C++ 中的所有字节 - 一个无符号字符。除此之外,是的,我会投......但这个演员更好:

unsigned_char_arr[i]= static_cast<unsigned char>(char_arr[i]);

or... just use the char array and cast it when it needs to be interpreted as an unsigned char...

或者...只需使用 char 数组并在需要将其解释为无符号字符时进行转换...

回答by Adam Rosenfield

In almost every C++ implementation you'll come across, a charis exactly a bytean octet. This is not guaranteed by the C++ standard, but it's practically always the case. A charis always at least8 bits large, and the exact number of bits is given by the preprocessor constant CHAR_BIT. Also, the sizeof()operator tells you the size of an object/type in terms of the number of chars, not the number of bytesoctets, so if you were on some weird system with a 16-bit charand a 32-bit int, then sizeof(int)would be 2, not 4.

几乎在每一个C ++实现,你会遇到,一个char正是一个byte八位字节。C++ 标准不保证这一点,但实际上总是如此。Achar总是至少8 位大,确切的位数由预处理器常数 给出CHAR_BIT。此外,sizeof()运算符会根据chars的数量而不是字节的八位字节数告诉您对象/类型的大小,因此如果您使用的是具有 16 位char和 32 位的奇怪系统int,那么sizeof(int)将是2,不是 4。

EDIT:Replaced byte by octet. A charis guaranteed to be a byte by the C standard, but a byte is not guaranteed to be an octet, which is exactly 8 bits. If you've ever read any French technical literature, they always use 'octet' instead of 'byte', and they have kilooctets (KO), megaoctets (MO), etc. instead of kilbytes and megabytes.

编辑:用八位字节替换字节。charC 标准保证A是一个字节,但不保证一个字节是一个八位字节,它正好是 8 位。如果您曾经阅读过任何法国技术文献,他们总是使用“八位字节”而不是“字节”,并且他们有千字节 (KO)、兆字节 (MO) 等,而不是千字节和兆字节。