C++ 将 UINT32 值转换为 UINT8 数组[4]
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6499183/
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
Converting a UINT32 value into a UINT8 array[4]
提问by Chris
My question is how do you convert a UINT32 value to a UINT8 array[4] (C/C++) preferably in a manner independent of endianness? Additionally, how would you reconstruct the UINT32 value from the UINT8 array[4], to get back to where you started?
我的问题是如何最好以独立于字节序的方式将 UINT32 值转换为 UINT8 数组 [4] (C/C++)?此外,您将如何从 UINT8 数组 [4] 重建 UINT32 值,以回到您开始的地方?
回答by Alnitak
You haven't really said what you mean by independent of endianness- it's unclear since the byte array must have someendianness. That said, oneof the below must answer your requirements:
您还没有真正说出独立于字节序的含义- 目前尚不清楚,因为字节数组必须具有某些字节序。这就是说,一个的下方必须回答您的要求:
Given UINT32 v
and UINT8 a[4]
:
给定UINT32 v
和UINT8 a[4]
:
"Host" endian
“主机”字节序
(use the machine's native byte order):
(使用机器的本机字节顺序):
UINT8 *vp = (UINT8 *)&v;
a[0] = vp[0];
a[1] = vp[1];
a[2] = vp[2];
a[3] = vp[3];
or:
或者:
memcpy(a, &v, sizeof(v));
or:
或者:
*(UINT32 *)a = v;
Big endian
大端
(aka "network order"):
(又名“网络订单”):
a[0] = v >> 24;
a[1] = v >> 16;
a[2] = v >> 8;
a[3] = v;
Little endian
小端
a[0] = v;
a[1] = v >> 8;
a[2] = v >> 16;
a[3] = v >> 24;
回答by Patrick
E.g. like this:
例如像这样:
UINT32 value;
UINT8 result[4];
result[0] = (value & 0x000000ff);
result[1] = (value & 0x0000ff00) >> 8;
result[2] = (value & 0x00ff0000) >> 16;
result[3] = (value & 0xff000000) >> 24;
Edit:added parenthesis (>> seems to have higher precedence than &)
编辑:添加括号(>> 似乎比 & 具有更高的优先级)
回答by Timo Geusch
If you don't want to code it yourself, you can use the C library function htonl()to convert the 32-bit int to network byte order. There is also the function ntohl()to convert them back to host order.
如果不想自己编码,可以使用C库函数htonl()将32位int转换为网络字节序。还有函数ntohl()将它们转换回主机顺序。
Once they're in network byte order, it's simply a matter of accessing the int/long as a byte array.
一旦它们处于网络字节顺序,只需将 int/long 作为字节数组访问即可。
All in all that's are probably the most portable and tested way of achieving your goal.
总而言之,这可能是实现目标的最便携和经过测试的方式。
回答by amrelk
One could also do it with pointers. (This is little endian, but if you use the same reconstruction method it won't matter)
人们也可以用指针来做到这一点。(这是小端,但如果你使用相同的重构方法就无所谓了)
uint32 in = 0x12345678;
uint8 out[4];
*(uint32*)&out = in;
This assigns the value of the uint32 to the 4 bytes after the memory address of the uint8, doing exactly what you need.
这将 uint32 的值分配给 uint8 的内存地址之后的 4 个字节,这正是您需要的。
To go the other way:
走另一条路:
uint8 in[4] = {0x78, 0x56, 0x34, 0x12};
uint32 out;
out = *(uint32*)&in
回答by luja
use a Union consisting of an Array with 4 time uint8 and an uint32.
使用由具有 4 个时间 uint8 和 uint32 的数组组成的联合。
So it sorts automatically by c inherent pointer Magic (Arrays are pointers to start of array)
所以它按 c 固有指针 Magic 自动排序(数组是指向数组开头的指针)