简体   繁体   English

如何将char转换为unsigned int?

[英]How can I cast a char to an unsigned int?

I have a char array that is really used as a byte array and not for storing text. 我有一个char数组,它实际上用作字节数组,而不用于存储文本。 In the array, there are two specific bytes that represent a numeric value that I need to store into an unsigned int value. 在数组中,有两个特定的字节代表一个数字值,我需要将其存储为一个无符号的int值。 The code below explains the setup. 以下代码说明了该设置。

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. 感谢Martin B的快速回复,但这无法正常工作。 Specifically, in my case the two bytes are 0x00 and 0xbc . 具体来说,在我的情况下,这两个字节是0x000xbc Obviously what I want is 0x000000bc . 显然我想要的是0x000000bc But what I'm getting in my unsigned int is 0xffffffbc . 但是我得到的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 (.ie positive signed char values.) Martin发布的代码是我的原始代码,可以正常工作,只要所有字节均小于128(即带正负号的char值)。

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). 现在...这里有趣的事情肯定是运算符的顺序(多年以来,我仍然只记得+, -, * and / ...感到羞耻:-),所以我总是尽可能多地放括号) 。 [] is king. []王者】。 Second is the (cast) . 其次是(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) (如果您使用+而不是| ,请记住+<<重要,所以您需要刹车)

We don't need to upcast to (unsigned integer) the two (unsigned char) because there is the integral promotion that will do it for us for one, and for the other it should be an automatic Arithmetic Conversion . 我们不需要将两个(unsigned char)转换为(unsigned integer) ,因为有一个整数提升将对我们有利,而对于另一个,它应该是自动算术转换

I'll add that if you want less headaches: 如果您想减少头痛,我会补充一点:

unsigned int val = (unsigned char)bytes[0] << CHAR_BIT;
val |= (unsigned char)bytes[1];
unsigned int val = (unsigned char) bytes[0]<<8 | (unsigned char) bytes[1];

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,它将在big-endian上工作):

unsigned int val = ntohs(*(uint16_t*)bytes)
unsigned int val = bytes[0] << 8 + bytes[1];

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM