|
- package com.telpo.dipperposition.common;
-
- /**
- * @program: dipperposition
- * @description: 16进制处理
- * @author: linwl
- * @create: 2021-01-14 22:05
- **/
- public class HexConvert {
- public static String convertStringToHex(String str){
-
- char[] chars = str.toCharArray();
-
- StringBuffer hex = new StringBuffer();
- for(int i = 0; i < chars.length; i++){
- hex.append(Integer.toHexString((int)chars[i]));
- }
-
- return hex.toString();
- }
-
- public static String convertHexToString(String hex){
-
- StringBuilder sb = new StringBuilder();
- StringBuilder sb2 = new StringBuilder();
-
- for( int i=0; i<hex.length()-1; i+=2 ){
-
- String s = hex.substring(i, (i + 2));
- int decimal = Integer.parseInt(s, 16);
- sb.append((char)decimal);
- sb2.append(decimal);
- }
-
- return sb.toString();
- }
- public static byte[] hexStringToBytes(String hexString) {
- if (hexString == null || hexString.equals("")) {
- return null;
- }
- // toUpperCase将字符串中的所有字符转换为大写
- hexString = hexString.toUpperCase();
- int length = hexString.length() / 2;
- // toCharArray将此字符串转换为一个新的字符数组。
- 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;
- }
- //返回匹配字符
- private static byte charToByte(char c) {
- return (byte) "0123456789ABCDEF".indexOf(c);
- }
-
- //将字节数组转换为short类型,即统计字符串长度
- public static short bytes2Short2(byte[] b) {
- short i = (short) (((b[1] & 0xff) << 8) | b[0] & 0xff);
- return i;
- }
-
- //将字节数组转换为16进制字符串
- public static String BinaryToHexString(byte[] bytes) {
- String hexStr = "0123456789ABCDEF";
- String result = "";
- String hex = "";
- for (byte b : bytes) {
- hex = String.valueOf(hexStr.charAt((b & 0xF0) >> 4));
- hex += String.valueOf(hexStr.charAt(b & 0x0F));
- result += hex + " ";
- }
- return result;
- }
-
- //將10進制轉換為16進制
- public static String encodeHEX(long numb){
-
- String hex= Long.toHexString(numb);
- return hex;
-
- }
-
-
- //將16進制字符串轉換為10進制數字
- public static long decodeHEX(String hexs){
- long longValue= Long.parseLong("123ABC", 16);
- return longValue;
- }
-
-
- /**
- * 生成校验码的int值
- * */
- public static String makeChecksum(String data) {
- if (data == null || data.equals("")) {
- return "";
- }
- int total = 0;
- int len = data.length();
- int num = 0;
- while (num < len) {
- String s = data.substring(num, num + 2);
- //System.out.println(s);
- total += Integer.parseInt(s, 16);
- num = num + 2;
- }
- /**
- * 用256求余最大是255,即16进制的FF
- */
- int mod = total % 256;
- String hex = Integer.toHexString(mod);
- len = hex.length();
- // 如果不够校验位的长度,补0,这里用的是两位校验
- if (len < 2) {
- hex = "0" + hex;
- }
- return hex;
- }
-
-
- /**
- * 生成校验码的int值
- * */
- public static String makeChecksumForBytes(byte[] byteDatas) {
- if (byteDatas == null || byteDatas.length == 0) {
- return "";
- }
- int total = 0;
- int len = byteDatas.length;
-
- final String HEX = "0123456789abcdef";
- StringBuilder sb = null;
- for (byte b : byteDatas) {
- sb = new StringBuilder(2);
- // 取出这个字节的高4位,然后与0x0f与运算,得到一个0-15之间的数据,通过HEX.charAt(0-15)即为16进制数
- sb.append(HEX.charAt((b >> 4) & 0x0f));
- // 取出这个字节的低位,与0x0f与运算,得到一个0-15之间的数据,通过HEX.charAt(0-15)即为16进制数
- sb.append(HEX.charAt(b & 0x0f));
- total += Integer.parseInt(sb.toString(), 16);
- }
- /**
- * 用256求余最大是255,即16进制的FF
- */
- int mod = total % 256;
- String hex = Integer.toHexString(mod);
- len = hex.length();
- // 如果不够校验位的长度,补0,这里用的是两位校验
- if (len < 2) {
- hex = "0" + hex;
- }
- return hex;
- }
- //
- // public static void main(String[] args) {
- //
- //
- // System.out.println("======ASCII码转换为16进制======");
- // String str = "*00007VERSION\\n1$";
- // System.out.println("字符串: " + str);
- // String hex = HexConvert.convertStringToHex(str);
- // System.out.println("====转换为16进制=====" + hex);
- //
- // System.out.println("======16进制转换为ASCII======");
- // System.out.println("Hex : " + hex);
- // System.out.println("ASCII : " + HexConvert.convertHexToString(hex));
- //
- // byte[] bytes = HexConvert.hexStringToBytes( hex );
- //
- // System.out.println(HexConvert.BinaryToHexString( bytes ));
- // }
-
- }
|