Ble数据格式转化的工具类

Posted by PanMin Blog on January 11, 2017

– layout: post # 使用的布局(不需要改) title: BLE数据格式转化的工具类 # 标题 subtitle: #副标题 date: 2017-01-11 # 时间 author: PanMin # 作者 header-img: img/post-bg-2015.jpg #这篇文章标题背景图片 catalog: true # 是否归档 tags: #标签 - Android —

两个byte –>int

private  int byteToInt(byte b, byte c) {//计算总包长,两个字节表示的
    short s = 0;
    int ret;
    short s0 = (short) (c & 0xff);// 最低位
    short s1 = (short) (b & 0xff);
    s1 <<= 8;
    s = (short) (s0 | s1);
    ret = s;
    return ret;
}

int –>两个byte

private byte[] int2byte(int res) {
    byte[] targets = new byte[2];
    targets[1] = (byte) (res & 0xff);// 最低位
    targets[0] = (byte) ((res >> 8) & 0xff);// 次低位
    return targets;
}

16进制字符串 –>byte[ ]

public static byte[] hexStringToByte(String hex) {
    int len = (hex.length() / 2);
    byte[] result = new byte[len];
    char[] achar = hex.toCharArray();
    for (int i = 0; i < len; i++) {
        int pos = i * 2;
        result[i] = (byte) (toByte(achar[pos]) << 4 | toByte(achar[pos + 1]));
    }
    return result;
}
private static byte toByte(char c) {
    byte b = (byte) "0123456789ABCDEF".indexOf(c);
    return b;
}

byte[ ] –>16进制字符串

 public static String byte2hex(byte [] buffer){
        String h = "";
        for(int i = 0; i < buffer.length; i++){
            String temp = Integer.toHexString(buffer[i] & 0xFF);
            if(temp.length() == 1){
                temp = "0" + temp;
            }
            h = h + temp;
        }
        return h;
  }