上海交通大学论坛|源来于此

 找回密码
 注册[30秒完成]
搜索
查看: 79308|回复: 0
打印 上一主题 下一主题

android开发蓝牙技术贴

[复制链接]
跳转到指定楼层
1#
发表于 2016-10-21 12:32:11 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
Android BLE 数据转换方法集合

(1)  byte[] 数组转成 16进制字符串

        private final static byte[] hex = "0123456789ABCDEF".getBytes();

        // 从字节数组到十六进制字符串转
        public static String Bytes2HexString(byte[] b) {
                byte[] buff = new byte[2 * b.length];
                for (int i = 0; i < b.length; i++) {
                        buff[2 * i] = hex[(b[i] >> 4) & 0x0f];
                        buff[2 * i + 1] = hex[b[i] & 0x0f];
                }
                return new String(buff);
        }

        示例:
byte[] bytes_1=new byte[]{(byte) 0xA0,(byte) 0xB1,0x2};
String hexStr=Conversion.Bytes2HexString(bytes_1);
LogUtils.print("hexStr:"+hexStr);

结果:hexStr:A0B102

(2)  16进制字符串转成 byte[] 数组
    
public static byte[] hexStringToBytes(String hexString) {
                if (hexString == null || hexString.equals("")) {
                        return null;
                }
                hexString = hexString.toUpperCase();
                int length = hexString.length() / 2;
                char[] hexChars = hexString.toCharArray();
                byte[] d = new byte[length];
                for (int i = 0; i < length; i++) {
                        int pos = i * 2;
                        d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
                }
                return d;
}

示例:
String strvalue = "A0B102";
byte[] bytes_2=Conversion.hexStringToBytes(strvalue);
String hexStr=Conversion.Bytes2HexString(bytes_2);
LogUtils.print("hexStr:"+hexStr);

结果:hexStr:A0B102


(3)  16进制字符串转成  int 整数
public static int byteStrToInt(String valueStr) {
                valueStr = valueStr.toUpperCase();
                if (valueStr.length() % 2 != 0) {
                        valueStr = "0" + valueStr;
                }

                int returnValue = 0;

                int length = valueStr.length();

                for (int i = 0; i < length; i++) {

                        int value = charToByte(valueStr.charAt(i));

                        returnValue += Math.pow(16, length - i - 1) * value;
                }
                return returnValue;
}
public static byte charToByte(char c) {
  return (byte) "0123456789ABCDEF".indexOf(c);
}
示例:
String strvalue = "A0B102";
int  value=Conversion.byteStrToInt(strvalue);
LogUtils.print("       value:"+value);
LogUtils.print("0xA0B102:"+0xA0B102);

结果:
     hexStr:10531074
   0xA0B102:10531074


(5)  byte转bit
public static String byteToBit(byte b) {  
            return "" +(byte)((b >> 7) & 0x1) +   
            (byte)((b >> 6) & 0x1) +   
            (byte)((b >> 5) & 0x1) +   
            (byte)((b >> 4) & 0x1) +   
            (byte)((b >> 3) & 0x1) +   
            (byte)((b >> 2) & 0x1) +   
            (byte)((b >> 1) & 0x1) +   
            (byte)((b >> 0) & 0x1);  
}  
(6)  bit转byte

public static byte BitToByte(String byteStr) {  
            int re, len;  
            if (null == byteStr) {  
                return 0;  
            }  
            len = byteStr.length();  
            if (len != 4 && len != 8) {  
                return 0;  
            }  
            if (len == 8) {// 8 bit处理  
                if (byteStr.charAt(0) == '0') {// 正数  
                    re = Integer.parseInt(byteStr, 2);  
                } else {// 负数  
                    re = Integer.parseInt(byteStr, 2) - 256;  
                }  
            } else {//4 bit处理  
                re = Integer.parseInt(byteStr, 2);  
            }  
            return (byte) re;  
}

您需要登录后才可以回帖 登录 | 注册[30秒完成]

本版积分规则

手机访问本页请
扫描左边二维码
         本网站声明
本网站所有内容为网友上传,若存在版权问题或是相关责任请联系站长!
站长联系QQ:7123767   myubbs.com
         站长微信:7123767
请扫描右边二维码
www.myubbs.com

小黑屋|手机版|Archiver|上海交通大学论坛 ( 渝ICP备17000839号-6 )

GMT+8, 2024-4-24 20:19 , Processed in 0.040527 second(s), 14 queries .

Powered by 高考信息网 X3.3

© 2001-2013 大学排名

快速回复 返回顶部 返回列表