打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Java byte数组转有符号int

前言

最近在写Java蓝牙串口通信读取数据中,需要将读取到的十六进制数转换为16位的有符号整数,网上Google一大堆,均无果,最后在StackOverFlow上找到了答案。

问题描述

通信协议上指出需要需要解析出三个16位的int型数据,每个数据2个字节,即2个byte。

温习一下java数据类型基础知识: byte型占1个字节,int型占4个字节,在java中没有无符号的数据类型。

下面是官方给的C#示例程序代码:


虽然没学过C#,但还是可以看出处理过程:
首先从原始数据byteTemp取出有用的部分复制到raw字节数组中,也就是raw中存放需要处理的两个字节,再调用自带的数组转置函数倒转一下顺序(应该是通信协议的要求),最后关键的一步调用BitConverter.ToInt16将2个字节转成一个16位的有符号整型数据。可见C#的各种小工具类还是挺全的,唉,此处不忍心要吐槽java了…
那么问题来了,如何将2个字节的字节数组转换成有符号的整型数据呢?也就是C#自带的API: BitConverter.ToInt16(byte[] src, int startIndex)在java中应该如何手动实现呢?

查看MSDN

打开微软的官方API文档想一探究竟,结果被吓得怀疑人生了,怀疑自己到底有没有学过计算机…这究竟怎么转的啊…怎么0F00就是15,F1FF就变成-15呢,郁闷…

Google搜索

无奈,先查阅各种资料,补充了点基础知识,学到了一些基本转换。

byte转换为int

两种情况:
1. 保持值不变,如进行数值运算,采用强制转换: int aInt = (int) aByte;
2. 保持低位不变,高位用0填充,如编码解码操作,采用位操作: int aInt = aByte & 0xFF;

int转换为byte

直接使用强制类型转换: byte aByte = (byte) aInt; 但注意前提是int值小于255

有符号byte数组与int相互转换

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找到答案

说实话,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));
  }
}

总结

  1. 不要小看基本数据类型以及各种位运算,这些往往才真的是难点,特别是对于java这种稀缺基本数据类型小工具包的语言
  2. 不要逃避问题,善用搜索引擎,还有给StackOverFlow点个赞
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Java中byte数组与long数组相互转化
Java 程序编码规范
Big Endian and Little Endian(字节序)
byte[]数组和int之间的转换
Java 位运算符
byte数组处理工具类分享,字节数组拼接,字节数组拆分
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服