我有一个十六进制值存储在一个两个字节的数组中:
unsigned char hex[2] = {0x02, 0x00};
如何将这个转换为十进制数值?
我有一个十六进制值存储在一个两个字节的数组中:
unsigned char hex[2] = {0x02, 0x00};
您可以使用(位运算)
int b =(hex [0]<< 8)| hex [1];
或(简单的数学运算)
int b =(hex [0] * 0x100)+ hex [1];
取决于大小端,但大概是这样的吧?
short value = (hex[0] << 16) & hex[1];
这不是一种高效的方法,至少我认为不是,但它可以在所有情况下工作,无论数组的大小,并且很容易转换为b。
__int8 p[] = {1, 1, 1, 1, 1}; //Let's say this was the array used.
int value = sizeof(p); //Just something to store the length.
memcpy(&value, &p, value > 4 ? 4 : value); //It's okay to go over 4, but might as well limit it.
int value = p[0] | p[1] << 0x8 | p[2] << 0x10 | p[3] << 0x18;