北斗定位
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

175 lines
5.3KB

  1. package com.telpo.dipperposition.common;
  2. /**
  3. * @program: dipperposition
  4. * @description: 16进制处理
  5. * @author: linwl
  6. * @create: 2021-01-14 22:05
  7. **/
  8. public class HexConvert {
  9. public static String convertStringToHex(String str){
  10. char[] chars = str.toCharArray();
  11. StringBuffer hex = new StringBuffer();
  12. for(int i = 0; i < chars.length; i++){
  13. hex.append(Integer.toHexString((int)chars[i]));
  14. }
  15. return hex.toString();
  16. }
  17. public static String convertHexToString(String hex){
  18. StringBuilder sb = new StringBuilder();
  19. StringBuilder sb2 = new StringBuilder();
  20. for( int i=0; i<hex.length()-1; i+=2 ){
  21. String s = hex.substring(i, (i + 2));
  22. int decimal = Integer.parseInt(s, 16);
  23. sb.append((char)decimal);
  24. sb2.append(decimal);
  25. }
  26. return sb.toString();
  27. }
  28. public static byte[] hexStringToBytes(String hexString) {
  29. if (hexString == null || hexString.equals("")) {
  30. return null;
  31. }
  32. // toUpperCase将字符串中的所有字符转换为大写
  33. hexString = hexString.toUpperCase();
  34. int length = hexString.length() / 2;
  35. // toCharArray将此字符串转换为一个新的字符数组。
  36. char[] hexChars = hexString.toCharArray();
  37. byte[] d = new byte[length];
  38. for (int i = 0; i < length; i++) {
  39. int pos = i * 2;
  40. d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
  41. }
  42. return d;
  43. }
  44. //返回匹配字符
  45. private static byte charToByte(char c) {
  46. return (byte) "0123456789ABCDEF".indexOf(c);
  47. }
  48. //将字节数组转换为short类型,即统计字符串长度
  49. public static short bytes2Short2(byte[] b) {
  50. short i = (short) (((b[1] & 0xff) << 8) | b[0] & 0xff);
  51. return i;
  52. }
  53. //将字节数组转换为16进制字符串
  54. public static String BinaryToHexString(byte[] bytes) {
  55. String hexStr = "0123456789ABCDEF";
  56. String result = "";
  57. String hex = "";
  58. for (byte b : bytes) {
  59. hex = String.valueOf(hexStr.charAt((b & 0xF0) >> 4));
  60. hex += String.valueOf(hexStr.charAt(b & 0x0F));
  61. result += hex + " ";
  62. }
  63. return result;
  64. }
  65. //將10進制轉換為16進制
  66. public static String encodeHEX(long numb){
  67. String hex= Long.toHexString(numb);
  68. return hex;
  69. }
  70. //將16進制字符串轉換為10進制數字
  71. public static long decodeHEX(String hexs){
  72. long longValue= Long.parseLong("123ABC", 16);
  73. return longValue;
  74. }
  75. /**
  76. * 生成校验码的int值
  77. * */
  78. public static String makeChecksum(String data) {
  79. if (data == null || data.equals("")) {
  80. return "";
  81. }
  82. int total = 0;
  83. int len = data.length();
  84. int num = 0;
  85. while (num < len) {
  86. String s = data.substring(num, num + 2);
  87. //System.out.println(s);
  88. total += Integer.parseInt(s, 16);
  89. num = num + 2;
  90. }
  91. /**
  92. * 用256求余最大是255,即16进制的FF
  93. */
  94. int mod = total % 256;
  95. String hex = Integer.toHexString(mod);
  96. len = hex.length();
  97. // 如果不够校验位的长度,补0,这里用的是两位校验
  98. if (len < 2) {
  99. hex = "0" + hex;
  100. }
  101. return hex;
  102. }
  103. /**
  104. * 生成校验码的int值
  105. * */
  106. public static String makeChecksumForBytes(byte[] byteDatas) {
  107. if (byteDatas == null || byteDatas.length == 0) {
  108. return "";
  109. }
  110. int total = 0;
  111. int len = byteDatas.length;
  112. final String HEX = "0123456789abcdef";
  113. StringBuilder sb = null;
  114. for (byte b : byteDatas) {
  115. sb = new StringBuilder(2);
  116. // 取出这个字节的高4位,然后与0x0f与运算,得到一个0-15之间的数据,通过HEX.charAt(0-15)即为16进制数
  117. sb.append(HEX.charAt((b >> 4) & 0x0f));
  118. // 取出这个字节的低位,与0x0f与运算,得到一个0-15之间的数据,通过HEX.charAt(0-15)即为16进制数
  119. sb.append(HEX.charAt(b & 0x0f));
  120. total += Integer.parseInt(sb.toString(), 16);
  121. }
  122. /**
  123. * 用256求余最大是255,即16进制的FF
  124. */
  125. int mod = total % 256;
  126. String hex = Integer.toHexString(mod);
  127. len = hex.length();
  128. // 如果不够校验位的长度,补0,这里用的是两位校验
  129. if (len < 2) {
  130. hex = "0" + hex;
  131. }
  132. return hex;
  133. }
  134. //
  135. // public static void main(String[] args) {
  136. //
  137. //
  138. // System.out.println("======ASCII码转换为16进制======");
  139. // String str = "*00007VERSION\\n1$";
  140. // System.out.println("字符串: " + str);
  141. // String hex = HexConvert.convertStringToHex(str);
  142. // System.out.println("====转换为16进制=====" + hex);
  143. //
  144. // System.out.println("======16进制转换为ASCII======");
  145. // System.out.println("Hex : " + hex);
  146. // System.out.println("ASCII : " + HexConvert.convertHexToString(hex));
  147. //
  148. // byte[] bytes = HexConvert.hexStringToBytes( hex );
  149. //
  150. // System.out.println(HexConvert.BinaryToHexString( bytes ));
  151. // }
  152. }