转换字节数组的部分为整数值 [英] Converting sections of byte arrays to integer values

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

问题描述

我目前正在接受来自设备的蓝牙测试Android应用程序。该数据被打包的方法是在4个字节。我需要两个值这些字节。

I am currently making an android application which accepts bluetooth measurement from a device. The way the data is packaged is in 4 bytes. I need to get two values out of these bytes.

的第一个值是由:6bit的和第一字节和位0的7位至字节2的位6

First value is made up of: 6bit and 7bit of first byte and bit 0 to bit 6 of byte 2

二值较为简单,包括满第三个字节的。

Second values is simpler and consists of the full 3rd byte.

什么是访问这些位值,将它们结合起来,并将其转换为整数值的好办法?现在,我想从字节数组转换为位集合,然后访问各个位创造那么这将被转换为一个整数新的字节。

What is a good way to access these bit values, combine them and convert them to integer values? Right now i'm trying to convert from byte array to bitset, and then access individual bits to create new bytes that would then be converted to a integer.

感谢,并请问,如果我没有说清楚就好了。

Thanks, and please ask if I am not being clear enough.

推荐答案

林不知道如果我理解正确的格式。
这是否掩码符合您的第一个值?

im not sure if i understood your format correctly. Does this bitmask correspond to your first value?

0xC07F0000

这是位16-22,30,31(这里使用基于零的索引,即第31位是最后一位)。

That is bits 16-22,30,31 (zero based indexing used here, i.e. bit 31 is last bit).

另一件事,是你的价值预计将签署或无符号的?

Another thing, is your value expected to be signed or unsigned?

无论如何,如果是我假设,那么你可以将其与一些位掩码转换这样的方式:

Anyway, if it is the way i assume then you can convert it like this with some bitmasks:

unsigned int val = 0xdeadbeef;

unsigned int mask1 = 0xC0000000;
unsigned int mask2 = 0x007F0000;

unsigned int YourValue = (val&mask1)>>23 | (val&mask2)>>16;

做,在与您的其他值的方法相同。定义一个位掩码并将其转移到右侧。完成。

Do that in the same way with your other values. Define a bitmask and shift it to the right. Done.

干杯

这篇关于转换字节数组的部分为整数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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