package com.dayu.rechargeqh.tools; import java.math.BigInteger; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.util.Arrays; import java.util.Collections; import java.util.List; /** * Copyright (C), 2022, * Author: zuo * Date: 2022/3/9 14:55 * Description: 进制转换 */ public class HexUtil { /** * hex字符串转byte数组 * * @param inHex 待转换的Hex字符串 * @return 转换后的byte数组结果 */ public static byte[] hexToByteArray(String inHex) { int hexlen = inHex.length(); byte[] result; if (hexlen % 2 == 1) { //奇数 hexlen++; result = new byte[(hexlen / 2)]; inHex = "0" + inHex; } else { //偶数 result = new byte[(hexlen / 2)]; } int j = 0; for (int i = 0; i < hexlen; i += 2) { result[j] = hexToByte(inHex.substring(i, i + 2)); j++; } return result; } public static int getBcdToInt(byte data) { return ((data & 0xF0) >> 4) * 10 + ((data & 0x0F)); } /** * Hex字符串转byte * * @param inHex 待转换的Hex字符串 * @return 转换后的byte */ public static byte hexToByte(String inHex) { return (byte) Integer.parseInt(inHex, 16); } /** * 字节转十六进制 * * @param b 需要进行转换的byte字节 * @return 转换后的Hex字符串 */ public static String byteToHex(byte b) { String hex = Integer.toHexString(b & 0xFF); if (hex.length() < 2) { hex = "0" + hex; } return hex.toUpperCase(); } /** * 字节数组转16进制 * * @param bytes 需要转换的byte数组 * @return 转换后的Hex字符串 */ public static String bytesToHex(byte[] bytes) { StringBuffer sb = new StringBuffer(); for (int i = 0; i < bytes.length; i++) { String hex = Integer.toHexString(bytes[i] & 0xFF); if (hex.length() < 2) { sb.append(0); } sb.append(hex); } return sb.toString(); } public static String byteArrayToHexString(byte[] byteArray) { StringBuilder hexString = new StringBuilder(); for (byte b : byteArray) { // 将字节转换为无符号整数 int unsignedInt = b & 0xff; // 将无符号整数转换为16进制字符串 String hex = Integer.toHexString(unsignedInt); // 如果字符串长度小于2,在前面补0 if (hex.length() < 2) { hex = "0" + hex; } hexString.append(hex); } return hexString.toString(); } /** * 字节数组转16进制 不在末尾添加0 * * @param bytes 需要转换的byte数组 * @return 转换后的Hex字符串 */ public static String bytesToHexNoAddZero(byte[] bytes) { StringBuffer sb = new StringBuffer(); for (int i = 0; i < bytes.length; i++) { String hex = Integer.toHexString(bytes[i] & 0xFF); sb.append(hex); } return sb.toString(); } /** * 将 4字节的16进制字符串,转换为32位带符号的十进制浮点型 * * @param str 4字节 16进制字符 * @return */ public static float hexToFloat(String str) { return Float.intBitsToFloat(new BigInteger(str, 16).intValue()); } /** * 将带符号的32位浮点数装换为16进制 * * @param value * @return */ public static String folatToHexString(Float value) { return Integer.toHexString(Float.floatToIntBits(value)); } /** * 将带符号的32位浮点数装换byte数组 * 低位在前高位在后 * * @param value * @return */ public static byte[] folatToByte(Float value) { ByteBuffer buffer = ByteBuffer.allocate(4); // 4个字节 buffer.order(ByteOrder.LITTLE_ENDIAN); buffer.putFloat(value); byte[] byteArray = buffer.array(); return byteArray; } /** * 将byte数组转换为带符号的32位浮点数 * * 低位在前高位在后 * * @param value * @return */ public static Float bytesToFloat(byte[] value) { ByteBuffer bufferLittleEndian = ByteBuffer.wrap(value); bufferLittleEndian.order(ByteOrder.LITTLE_ENDIAN); return bufferLittleEndian.getFloat(); } /** * 十进制转16进制 * * @param number * @return */ public static String get10to16(int number) { return Integer.toHexString(number); } /** * 十进制转16进制 补齐偶数 高位在前低位在后 * * @param number * @return */ public static String get10to16CompleteHex(int number) { String hex = Integer.toHexString(number); if (hex.length() % 2 == 0) { return hex; } else { return "0" + hex; } } /** * short10进制转16进制 低位在前高位在后 * * @param number * @return */ public static String get10To16LowHigh(short number) { // 使用 Integer.toHexString 将 short 转换为十六进制字符串 int intValue = Short.toUnsignedInt(number); // 使用 Integer.toHexString 将 int 转换为十六进制字符串 String hexString = Integer.toHexString(intValue); // 补0,确保字符串长度为4 while (hexString.length() < 4) { hexString = "0" + hexString; } hexString = spaceHex(hexString); hexString = HighLowHex(hexString); return hexString; } /** * short10进制转16进制 低位在前高位在后 * * @param number * @return */ public static String get10To16LowHigh(int number) { // 使用 Integer.toHexString 将 int 转换为十六进制字符串 String hexString = Integer.toHexString(number); hexString = spaceHex(hexString); hexString = HighLowHex(hexString); return hexString; } /** * short16进制转10进制 低位在前高位在后 * * @param hex * @return */ public static short getShort10To16LowHigh(String hex) { try { String str = ""; str = spaceHex(hex); str = HighLowHex(str); return (short) Integer.parseInt(str, 16); } catch (NumberFormatException e) { e.printStackTrace(); } return 0; } /** * 十进制转16进制低位在前高位在后 * * @param number 十进制数 * @param length 补足多少位 * @return */ public static String get10to16LowHigh(int number, int length) { String str = ""; try { str = Integer.toHexString(number); str = getHexToLenght(str, length); str = spaceHex(str); str = HighLowHex(str); } catch (Exception e) { e.printStackTrace(); } return str; } /** * 16进制转10进制高低位转换 * * @param hex * @return */ public static int get16to10LowHigh(String hex) { try { String str = ""; str = spaceHex(hex); str = HighLowHex(str); return Integer.parseInt(str, 16); } catch (NumberFormatException e) { e.printStackTrace(); } return 0; } /** * 返回特定长度的16进制字符串 * * @param data * @param length * @return */ public static String getHexToLenght(String data, int length) { StringBuffer stringBuilder = new StringBuffer(data); for (int i = 0; i < length - data.length(); i++) { stringBuilder.insert(0, "0"); } return stringBuilder.toString(); } /** * 十六进制数隔空位 * * @param str * @return */ public static String spaceHex(String str) { char[] array = str.toCharArray(); if (str.length() <= 2) return str; StringBuffer bufferHex = new StringBuffer(); for (int i = 0; i < array.length; i++) { int start = i + 1; if (start % 2 == 0) { bufferHex.append(array[i]).append(" "); } else { bufferHex.append(array[i]); } } return bufferHex.toString(); } /** * 高位16进制转低位 * * @param str * @return */ private static String HighLowHex(String str) { if (str.trim().length() <= 2) return str; List list = Arrays.asList(str.split(" ")); Collections.reverse(list); StringBuffer stringBuffer = new StringBuffer(); for (String string : list) { stringBuffer.append(string); } return stringBuffer.toString(); } /** * @param hex * @return */ public static int get16to10(String hex) { int x = 0; try { x = Integer.parseInt(hex, 16); } catch (NumberFormatException e) { e.printStackTrace(); } return x; } }