最近在写Java蓝牙串口通信读取数据中,需要将读取到的十六进制数转换为16位的有符号整数,网上Google一大堆,均无果,最后在StackOverFlow上找到了答案。
通信协议上指出需要需要解析出三个16位的int型数据,每个数据2个字节,即2个byte。
温习一下java数据类型基础知识: byte型占1个字节,int型占4个字节,在java中没有无符号的数据类型。
下面是官方给的C#示例程序代码:
打开微软的官方API文档想一探究竟,结果被吓得怀疑人生了,怀疑自己到底有没有学过计算机…这究竟怎么转的啊…怎么0F00就是15,F1FF就变成-15呢,郁闷…
- Java 中int与byte数组转换详解 http://my.oschina.net/u/169390/blog/97495
- Java 中 byte、byte 数组和 int、long 之间的转换
http://blog.csdn.net/defonds/article/details/8782785- java中byte, int的转换
http://freewind886.blog.163.com/blog/static/661924642011810236100/
无奈,先查阅各种资料,补充了点基础知识,学到了一些基本转换。
两种情况:
1. 保持值不变,如进行数值运算,采用强制转换: int aInt = (int) aByte;
2. 保持低位不变,高位用0填充,如编码解码操作,采用位操作: int aInt = aByte & 0xFF;
直接使用强制类型转换: byte aByte = (byte) aInt;
但注意前提是int值小于255
public static int byteArrayToInt(byte[] b) {
return b[3] & 0xFF |
(b[2] & 0xFF) << 8 |
(b[1] & 0xFF) << 16 |
(b[0] & 0xFF) << 24;
}
public static byte[] intToByteArray(int a) {
return new byte[] {
(byte) ((a >> 24) & 0xFF),
(byte) ((a >> 16) & 0xFF),
(byte) ((a >> 8) & 0xFF),
(byte) (a & 0xFF)
};
}
// 任意长度
public static int toInt(byte[] bRefArr) {
int iOutcome = 0;
byte bLoop;
for (int i = 0; i < bRefArr.length; i++) {
bLoop = bRefArr[i];
iOutcome += (bLoop & 0xFF) << (8 * i);
}
return iOutcome;
}
说实话,StackOverFlow才是真正的神器啊!
Convert from 2 or 4 bytes to signed/unsigned short/int
http://stackoverflow.com/questions/10803557/convert-from-2-or-4-bytes-to-signed-unsigned-short-int
public class IntegerConversion
{
public static int convertTwoBytesToInt1 (byte b1, byte b2) // signed
{
return (b2 << 8) | (b1 & 0xFF);
}
public static int convertFourBytesToInt1 (byte b1, byte b2, byte b3, byte b4)
{
return (b4 << 24) | (b3 & 0xFF) << 16 | (b2 & 0xFF) << 8 | (b1 & 0xFF);
}
public static int convertTwoBytesToInt2 (byte b1, byte b2) // unsigned
{
return (b2 & 0xFF) << 8 | (b1 & 0xFF);
}
public static long convertFourBytesToInt2 (byte b1, byte b2, byte b3, byte b4)
{
return (long) (b4 & 0xFF) << 24 | (b3 & 0xFF) << 16 | (b2 & 0xFF) << 8 | (b1 & 0xFF);
}
public static void main (String[] args)
{
byte b1 = (byte) 0xFF;
byte b2 = (byte) 0xFF;
byte b3 = (byte) 0xFF;
byte b4 = (byte) 0xFF;
System.out.printf ("%,14d%n", convertTwoBytesToInt1 (b1, b2));
System.out.printf ("%,14d%n", convertTwoBytesToInt2 (b1, b2));
System.out.printf ("%,14d%n", convertFourBytesToInt1 (b1, b2, b3, b4));
System.out.printf ("%,14d%n", convertFourBytesToInt2 (b1, b2, b3, b4));
}
}
联系客服