將字節數組的部分轉換為整數值 (Converting sections of byte arrays to integer values)


問題描述

將字節數組的部分轉換為整數值 (Converting sections of byte arrays to integer values)

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. 

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.


參考解法

方法 1:

I figured it out: I converted the byte array to int using this method 

public static int byteArrayToInt(byte[] b, int offset) { 
   int value = 0; 
   for (int i = 0; i < 4; i++) { 
      int shift = (4 - 1 - i) * 8; 
      value += (b[i + offset] & 0x000000FF) << shift; 
   } 
   return value; 
} 

Then I used the method pokey described. Thanks for your help!

方法 2:

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

0xC07F0000

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.

Cheers

(by gtdevelgtdevelpokey909)

參考文件

  1. Converting sections of byte arrays to integer values (CC BY-SA 3.0/4.0)

#bytearray #string #integer #Android #bluetooth






相關問題

更改創建 XML 閱讀器時使用的 XmlDictionaryReaderQuotas 對象的 MaxArrayLength 屬性 (Changing the MaxArrayLength property on the XmlDictionaryReaderQuotas object used when creating the XML reader)

將文件讀入 4 個字節的 ByteArrays (Read file into ByteArrays of 4 bytes)

使用套接字 (C#) 進行文件傳輸 - 接收到的文件不包含完整數據 (File transfer using sockets (C#) - received file doesn't contain full data)

將 Java 字節讀取器轉換為 InputStream (Converting a Java byte reader to an InputStream)

用 bigInteger 和其他方法重建字節數組 (Rebuild byte array with bigInteger and other method)

如何將字節數組作為參數發送到 HTML.Action? (How to send byte array as paramater to HTML.Action?)

如何將存儲在字節數組中的圖像加載到 WebView? (How to load an image stored in a byte array to WebView?)

我希望將畫布保存為 mySql blob 字段中的 blob (I am looking to save a canvas as a blob in mySql blob field)

將字節數組的部分轉換為整數值 (Converting sections of byte arrays to integer values)

從內存中釋放 jni 引用 (Freeing jni references from memory)

為什麼 JPGEncoded bytearray 從 AS3 發送到 AMFPHP 會導致圖像無效? (Why is JPGEncoded bytearray sent from AS3 to AMFPHP resulting in an invalid image?)

Java:字節到浮點數/整數 (Java: Bytes to floats / ints)







留言討論