C语言 如何将字符转换为无符号整数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7893128/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I cast a char to an unsigned int?
提问by RLH
I have a char array that is really used as a byte array and not for storing text. In the array, there are two specific bytes that represent a numeric value that I need to store into an unsigned int value. The code below explains the setup.
我有一个真正用作字节数组而不是用于存储文本的字符数组。在数组中,有两个特定字节表示我需要存储到无符号 int 值中的数值。下面的代码解释了设置。
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;
I can not figure out how to do this. I would assume it would involve some casting and recasting of the pointers, but I can not get this to work. How can I accomplish my end goal?
我不知道如何做到这一点。我认为它会涉及一些指针的转换和重新转换,但我无法让它工作。我怎样才能实现我的最终目标?
UPDATE
更新
Thank you Martin B for the quick response, however this doesn't work. Specifically, in my case the two bytes are 0x00and 0xbc. Obviously what I want is 0x000000bc. But what I'm getting in my unsigned int is 0xffffffbc.
感谢 Martin B 的快速回复,但这不起作用。具体来说,在我的情况下,两个字节是0x00和0xbc。显然我想要的是0x000000bc. 但是我在我的 unsigned int 中得到的是0xffffffbc.
The code that was posted by Martin was my actual, original code and works fine so long as all of the bytes are less than 128 (.i.e. positive signed char values.)
Martin 发布的代码是我实际的原始代码,只要所有字节都小于 128(即正符号字符值),它就可以正常工作。
回答by xanatos
unsigned int val = (unsigned char)bytes[0] << CHAR_BIT | (unsigned char)bytes[1];
This if sizeof(unsigned int) >= 2 * sizeof(unsigned char)(not something guaranteed by the C standard)
这如果sizeof(unsigned int) >= 2 * sizeof(unsigned char)(不是由 C 标准保证的东西)
Now... The interesting things here is surely the order of operators (in many years still I can remember only +, -, * and /... Shame on me :-), so I always put as many brackets I can). []is king. Second is the (cast). Third is the <<and fourth is the |(if you use the +instead of the |, remember that +is more importan than <<so you'll need brakets)
现在......这里有趣的事情肯定是运算符的顺序(很多年我仍然只记得+, -, * and /......我很惭愧:-),所以我总是尽可能多地加上括号)。[]是国王。其次是(cast)。第三个是<<,第四个是|(如果你使用+代替|,记住这+比<<你需要刹车更重要)
We don't need to upcast to (unsigned integer)the two (unsigned char)because there is the integral promotionthat will do it for us for one, and for the other it should be an automatic Arithmetic Conversion.
我们不需要向上转换(unsigned integer)到两者,(unsigned char)因为有一个积分提升可以为我们做一个,而对于另一个它应该是一个自动的Arithmetic Conversion。
I'll add that if you want less headaches:
如果你想减少头痛,我会补充一点:
unsigned int val = (unsigned char)bytes[0] << CHAR_BIT;
val |= (unsigned char)bytes[1];
回答by Jens Erat
unsigned int val = (unsigned char) bytes[0]<<8 | (unsigned char) bytes[1];
回答by TJD
The byte ordering depends on the endianness of your processor. You can do this, which will work on big or little endian machines. (without ntohs it will work on big-endian):
字节顺序取决于处理器的字节序。你可以这样做,这将适用于大端或小端机器。(没有 ntohs 它将适用于大端):
unsigned int val = ntohs(*(uint16_t*)bytes)
回答by Martin Beckett
unsigned int val = bytes[0] << 8 + bytes[1];
回答by Chuck
I think this is a better way to go about it than relying on pointer aliasing:
我认为这是一种比依赖指针别名更好的方法:
union {unsigned asInt; char asChars[2];} conversion;
conversion.asInt = 0;
conversion.asChars[0] = 0x0C;
conversion.asChars[1] = 0x88;
unsigned val = conversion.asInt;

