简体   繁体   English

将小端序列中的4个字节转换为无符号整数

[英]Converting 4 bytes in little endian order into an unsigned integer

I have a string of 256*4 bytes of data. 我有一串256 * 4字节的数据。 These 256* 4 bytes need to be converted into 256 unsigned integers. 这些256 * 4字节需要转换为256个无符号整数。 The order in which they come is little endian, ie the first four bytes in the string are the little endian representation of the first integer, the next 4 bytes are the little endian representation of the next integer, and so on. 它们来的顺序是小端,即字符串中的前四个字节是第一个整数的小端表示,接下来的4个字节是下一个整数的小端表示,依此类推。

What is the best way to parse through this data and merge these bytes into unsigned integers? 解析这些数据并将这些字节合并为无符号整数的最佳方法是什么? I know I have to use bitshift operators but I don't know in what way. 我知道我必须使用bitshift运算符,但我不知道以什么方式。

Hope this helps you 希望这对你有所帮助

unsigned int arr[256];
char ch[256*4] = "your string";
for(int i = 0,k=0;i<256*4;i+=4,k++)
{
arr[k] = ch[i]|ch[i+1]<<8|ch[i+2]<<16|ch[i+3]<<24;
}

Alternatively, we can use C/C++ casting to interpret a char buffer as an array of unsigned int. 或者,我们可以使用C / C ++强制转换将char缓冲区解释为unsigned int数组。 This can help get away with shifting and endianness dependency. 这可以帮助摆脱转移和字节序依赖性。

#include <stdio.h>
int main()
{
    char buf[256*4] = "abcd";
    unsigned int *p_int = ( unsigned int * )buf;
    unsigned short idx = 0;
    unsigned int val = 0;
    for( idx = 0; idx < 256; idx++ )
    {
        val = *p_int++;
        printf( "idx = %d, val = %d \n", idx, val );
    }
}

This would print out 256 values, the first one is idx = 0, val = 1684234849 (and all remaining numbers = 0). 这将打印出256个值,第一个是idx = 0,val = 1684234849(并且所有剩余的数字= 0)。

As a side note, "abcd" converts to 1684234849 because it's run on X86 (Little Endian), in which "abcd" is 0x64636261 (with 'a' is 0x61, and 'd' is 0x64 - in Little Endian, the LSB is in the smallest address). 作为旁注,“abcd”转换为1684234849因为它在X86(Little Endian)上运行,其中“abcd”是0x64636261('a'是0x61,'d'是0x64 - 在Little Endian中,LSB是在最小的地址)。 So 0x64636261 = 1684234849. 所以0x64636261 = 1684234849。

Note also, if using C++, reinterpret_cast should be used in this case: 另请注意,如果使用C ++,则应在此情况下使用reinterpret_cast

const char *p_buf = "abcd";
const unsigned int *p_int = reinterpret_cast< const unsigned int * >( p_buf );

If your host system is little-endian, just read along 4 bytes, shift properly and copy them to int 如果您的主机系统是little-endian,只需读取4个字节,正确移位并将它们复制到int

char bytes[4] = "....";
int i = bytes[0] | (bytes[1] << 8) | (bytes[2] << 16) | (bytes[3] << 24);

If your host is big-endian, do the same and reverse the bytes in the int, or reverse it on-the-fly while copying with bit-shifting, ie just change the indexes of bytes[] from 0-3 to 3-0 如果您的主机是big-endian,请执行相同操作并反转int中的字节,或者在使用位移复制时即时反转它,即只需将bytes[]的索引从0-3更改为3 0

But you shouldn't even do that just copy the whole char array to the int array if your PC is in little-endian 但是你甚至不应该这样做只是将整个char数组复制到int数组中,如果你的PC是小端的话

#define LEN 256
char bytes[LEN*4] = "blahblahblah";
unsigned int uint[LEN];
memcpy(uint, bytes, sizeof bytes);

That said, the best way is to avoid copying at all and use the same array for both types 也就是说,最好的方法是避免复制并对两种类型使用相同的数组

union
{
    char bytes[LEN*4];
    unsigned int uint[LEN];
} myArrays;

// copy data to myArrays.bytes[], do something with those bytes if necessary
// after populating myArrays.bytes[], get the ints by myArrays.uint[i]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM