字节转换为INT在C / UINT [英] Convert Bytes to Int / uint in C

查看:171
本文介绍了字节转换为INT在C / UINT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个无符号的字符数组[248];充满字节。像2F AF FF 00 EB AB CD EF .....
此Array是作为一个缓冲区,我店从UART(RS232)我的数据我的字节流。

I have a unsigned char array[248]; filled with bytes. Like 2F AF FF 00 EB AB CD EF ..... This Array is my Byte Stream which I store my Data from the UART (RS232) as a Buffer.

现在我想转换为字节回到我UINT16的和INT32的。

Now I want to convert the bytes back to my uint16's and int32's.

在C#中我用了BitConverter类来做到这一点。例如:

In C# I used the BitConverter Class to do this. e.g:

byte[] Array = { 0A, AB, CD, 25 };
int myint1 = BitConverter.ToInt32(bytes, 0);
int myint2 = BitConverter.ToInt32(bytes, 4);
int myint3 = BitConverter.ToInt32(bytes, 8);
int myint4 = BitConverter.ToInt32(bytes, 12);
//...
enter code here
Console.WriteLine("int: {0}", myint1); //output Data...

有没有用C一个类似的功能? (没有.NET,我用的是KEIL编译因为code是一个微控制器上运行)

Is there a similiar Function in C ? (no .net , I use the KEIL compiler because code is running on a microcontroller)

与问候
山姆

解决方案:
方法A)

Solutions: Way A)

首先,我不得不转换或初始化数组作为uint8_t有ARRAY [248];
然后我用这个code在您的帮助:

first I had to convert or initialize the array as an uint8_t ARRAY[248]; Then I used this code with your help:

 uint32_t* myint1 = (uint32_t *)&RXBUFF[2]; //test
 uint16_t* myint2 = (uint16_t *)&RXBUFF[6]; //test3

注意:
十六进制的int值1985年是myint2 psented为0x07C1重新$ P $。我sended的字节是C1 07这样的微控制器是改变字节顺序

Attention: The int value "1985" in hex is represented as 0x07C1 in myint2 . the Byte that I sended was "C1 07" so the Microcontroller is changing the Byte Order

我将测试其他的方法了。

I will test the other Methods too.

WR山姆:)

推荐答案

是的,有。假设你的字节是:

Yes there is. Assume your bytes are in:

uint8_t bytes[N] = { /* whatever */ };

我们知道,一个16位的整数,只不过是两个8位整数连接在一起,即一个具有256的倍数或可选择地被移8:

We know that, a 16 bit integer is just two 8 bit integers concatenated, i.e. one has a multiple of 256 or alternatively is shifted by 8:

uint16_t sixteen[N/2];

for (i = 0; i < N; i += 2)
    sixteen[i/2] = bytes[i] | (uint16_t)bytes[i+1] << 8;
             // assuming you have read your bytes little-endian

同样,对于32位:

Similarly for 32 bits:

uint32_t thirty_two[N/4];

for (i = 0; i < N; i += 4)
    thirty_two[i/4] = bytes[i] | (uint32_t)bytes[i+1] << 8
        | ((uint32_t)bytes[i+2] << 16 | (uint32_t)bytes[i+3] << 24;
             // same assumption


如果这些字节读大端,当然你颠倒了顺序:


If the bytes are read big-endian, of course you reverse the order:

bytes[i+1] | (uint16_t)bytes[i] << 8

bytes[i+3] | (uint32_t)bytes[i+2] << 8
    | (uint32_t)bytes[i+1] << 16 | (uint32_t)bytes[i] << 24

请注意,有一个在存储整数字节序和运行架构的字节序之间的差异。该字节序中提到的这个答案是存储整数,即,字节的内容。该解决方案是独立运行的架构,因为字节序是照顾转移时的字节序的。

Note that there's a difference between the endian-ness in the stored integer and the endian-ness of the running architecture. The endian-ness referred to in this answer is of the stored integer, i.e., the contents of bytes. The solutions are independent of the endian-ness of the running architecture since endian-ness is taken care of when shifting.

这篇关于字节转换为INT在C / UINT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆