[英]Converting 4 bytes to unsigned int
If one has a character array such as 如果有一个字符数组,例如
char bytes[256] = "10000011011110110010001101000011";
I want to unsigned value of this which would be : 2205885251
我想将其的无符号值是:
2205885251
I'm trying to do something along these lines 我正在尝试按照这些思路做一些事情
unsigned int arr[256];
for(int i = 0, k=0; i<256; i++, k++)
{
arr[k] = bytes[i]|bytes[i+1]<<8|bytes[i+2]<<16|bytes[i+3]<<24;
}
I am obtaining the wrong value: 3220856520
, can anyone point out what I am doing wrong? 我得到了错误的值:
3220856520
,谁能指出我做错了吗?
#include <stdio.h>
int main()
{
char bytes[256] = "10000011011110110010001101000011";
unsigned int value = 0;
for(int i = 0; i< 32; i++)
{
value = value *2 + (bytes[i]-'0');
}
printf("%u\n",value);
}
it outputs: 2205885251 它输出:2205885251
char bytes[] = "10000011011110110010001101000011";
unsigned int k;
k = strtoul(bytes, NULL, 2);
printf("%u \n", k);
valter 瓦尔特
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.