我有一个字符数组,实际上是用作字节数组而不是用来存储文本。在该数组中,有两个特定的字节表示我需要存储为无符号整数值的数字值。以下代码解释了设置。
char* bytes = bytes[2];
bytes[0] = 0x0C; // For the sake of this example, I'm
bytes[1] = 0x88; // assigning random values to the char array.
unsigned int val = ???; // This needs to be the actual numeric
// value of the two bytes in the char array.
// In other words, the value should equal 0x0C88;
我无法弄清楚如何做到这一点。我想这可能涉及指针的某些转换和重新转换,但我无法使其工作。我该如何实现我的最终目标?
更新
感谢Martin B的快速响应,但这并不起作用。具体来说,在我的情况下,这两个字节是0x00
和0xbc
。显然,我想要的是0x000000bc
。但我在我的无符号整数中得到的是0xffffffbc
。
Martin发布的代码是我的实际原始代码,只要所有字节都小于128(即正有符号字符值),它就可以正常工作。
unsigned int
的大小可能与char[2]
不同。 - Mooing Duck