如何将char转换为unsigned int?

时间:2021-06-25 06:51:37

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.

我有一个char数组,它实际上用作字节数组而不是用于存储文本。在数组中,有两个特定字节表示我需要存储到unsigned 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 0x00 and 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(.i.e。正签名char值),它就能正常工作。

5 个解决方案

#1


15  

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)

这个if 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)

现在......这里有趣的事情肯定是运营商的顺序(多年来我仍然记得只有+, - ,*和/ ......对我感到羞耻:-),所以我总是尽可能多地设置括号) 。 []为王。第二是(演员)。第三是“和第四是| (如果你使用+而不是|,请记住+比< <更重要,所以你需要使用brakets)< p>

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.

我们不需要向上转换为(无符号整数)两个(无符号整数),因为有一个整数提升将为我们做一个,而另一个应该是自动算术转换。

I'll add that if you want less headaches:

如果你想减少头痛,我会补充说:

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

#2


4  

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

#3


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)

#4


0  

unsigned int val = bytes[0] << 8 + bytes[1];

#5


0  

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;

#1


15  

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)

这个if 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)

现在......这里有趣的事情肯定是运营商的顺序(多年来我仍然记得只有+, - ,*和/ ......对我感到羞耻:-),所以我总是尽可能多地设置括号) 。 []为王。第二是(演员)。第三是“和第四是| (如果你使用+而不是|,请记住+比< <更重要,所以你需要使用brakets)< p>

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.

我们不需要向上转换为(无符号整数)两个(无符号整数),因为有一个整数提升将为我们做一个,而另一个应该是自动算术转换。

I'll add that if you want less headaches:

如果你想减少头痛,我会补充说:

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

#2


4  

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

#3


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)

#4


0  

unsigned int val = bytes[0] << 8 + bytes[1];

#5


0  

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;