当前位置:   article > 正文

Android Java byte数组 16进制与10进制之间转换_byte16进制转10进制

byte16进制转10进制

前段时间当好要做一个Ble 读写。

其中就有进制转换。

bytesToHex();//byte数据转化成16进制字符串
  1. static final char[] hexArray = "0123456789ABCDEF".toCharArray();
  2. private static String bytesToHex(byte[] bytes) {
  3. char[] hexChars = new char[bytes.length * 2];
  4. for (int j = 0; j < bytes.length; j++) {
  5. int v = bytes[j] & 0xFF;
  6. hexChars[j * 2] = hexArray[v >>> 4];
  7. hexChars[j * 2 + 1] = hexArray[v & 0x0F];
  8. }
  9. return new String(hexChars);
  10. }
  1. //剪切字符得到相应字节里的数据(例子有高低位之分的,高在前,底在后)
  2. String L=bytesToHex(value).substring(6,8);
  3. String H=bytesToHex(value).substring(8,10);
  4. long ten_num = Long.parseLong(H+L, 16);//把并接的16进制字符串转换成10进制long
String strHex = Integer.toHexString(125); //把int值转换成16进制字符串
  1. //把整数字符串,转变成byte字节
  2. public static byte hexToByte(String inHex) {
  3. return (byte) Integer.parseInt(inHex, 16);
  4. }
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/226766
推荐阅读
相关标签
  

闽ICP备14008679号