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];
}
4 个解决方案
#1
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的类型实际上不是一个字节大小,那就简直就是疯了。因此,简单的演员应该可以做到这一点。
#2
There is no byte type in C++, and according to the Standard:
C ++中没有字节类型,根据标准:
Edit:
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:
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; sizeof应用于任何其他基本类型(3.9.1)的结果是实现定义的。
#3
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。除此之外,是的,我会投...但这个演员阵容更好:
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数组并在需要将其解释为unsigned char时将其强制转换...
#4
In almost every C++ implementation you'll come across, a char
is exactly a an octet. This is not guaranteed by the C++ standard, but it's practically always the case. A byte
char
is always at least 8 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 char
s, not the number of bytes octets, so if you were on some weird system with a 16-bit char
and a 32-bit int
, then sizeof(int)
would be 2, not 4.
在几乎每个C ++实现中,您都会遇到,char只是一个八位字节的字节。这不是C ++标准所保证的,但实际上总是如此。 char总是至少8位大,并且精确的位数由预处理器常量CHAR_BIT给出。此外,sizeof()运算符根据字符数来告诉您对象/类型的大小,而不是字节数八位字节的数量,所以如果你在一些带有16位字符和32位字符的奇怪系统上int,那么sizeof(int)将是2,而不是4。
EDIT: Replaced byte by octet. A char
is 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.
编辑:用八位字节替换字节。根据C标准,char保证是一个字节,但不保证一个字节是八位字节,恰好是8位。如果您曾阅读过任何法国技术文献,他们总是使用“八位字节”而不是“字节”,并且它们具有千字节(KO),兆字节(MO)等,而不是千字节和兆字节。
#1
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的类型实际上不是一个字节大小,那就简直就是疯了。因此,简单的演员应该可以做到这一点。
#2
There is no byte type in C++, and according to the Standard:
C ++中没有字节类型,根据标准:
Edit:
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:
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; sizeof应用于任何其他基本类型(3.9.1)的结果是实现定义的。
#3
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。除此之外,是的,我会投...但这个演员阵容更好:
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数组并在需要将其解释为unsigned char时将其强制转换...
#4
In almost every C++ implementation you'll come across, a char
is exactly a an octet. This is not guaranteed by the C++ standard, but it's practically always the case. A byte
char
is always at least 8 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 char
s, not the number of bytes octets, so if you were on some weird system with a 16-bit char
and a 32-bit int
, then sizeof(int)
would be 2, not 4.
在几乎每个C ++实现中,您都会遇到,char只是一个八位字节的字节。这不是C ++标准所保证的,但实际上总是如此。 char总是至少8位大,并且精确的位数由预处理器常量CHAR_BIT给出。此外,sizeof()运算符根据字符数来告诉您对象/类型的大小,而不是字节数八位字节的数量,所以如果你在一些带有16位字符和32位字符的奇怪系统上int,那么sizeof(int)将是2,而不是4。
EDIT: Replaced byte by octet. A char
is 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.
编辑:用八位字节替换字节。根据C标准,char保证是一个字节,但不保证一个字节是八位字节,恰好是8位。如果您曾阅读过任何法国技术文献,他们总是使用“八位字节”而不是“字节”,并且它们具有千字节(KO),兆字节(MO)等,而不是千字节和兆字节。