将小字节顺序的4个字节转换为无符号整数 [英] Converting 4 bytes in little endian order into an unsigned integer

查看:163
本文介绍了将小字节顺序的4个字节转换为无符号整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个256 * 4字节的数据字符串。这些256 * 4字节需要转换为256个无符号整数。它们来的顺序是小端,即字符串中的前四个字节是第一个整数的小端表示,接下来的4个字节是下一个整数的小端表示,等等。

I have a string of 256*4 bytes of data. These 256* 4 bytes need to be converted into 256 unsigned integers. The order in which they come is little endian, i.e. 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.

通过此数据解析这些数据并将这些字节合并为无符号整数的最佳方法是什么?我知道我必须使用bitshift操作符,但我不知道以什么方式。

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.

推荐答案

希望这有助于

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;
}

这篇关于将小字节顺序的4个字节转换为无符号整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆