package com.dayu.baselibrary.tools;
|
|
import java.math.BigInteger;
|
import java.nio.ByteBuffer;
|
import java.nio.ByteOrder;
|
import java.util.ArrayList;
|
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 {
|
|
/**
|
* 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;
|
}
|
|
|
/**
|
* 16进制转10进制并且低位在前高位在后
|
*
|
* @param data
|
* @return
|
*/
|
public static int get16To10LowHightByBytes(byte[] data) {
|
List<Byte> byteList = new ArrayList<>();
|
for (byte b : data) {
|
byteList.add(b);
|
}
|
Collections.reverse(byteList);
|
byte[] byteArray = new byte[byteList.size()];
|
for (int i = 0; i < byteList.size(); i++) {
|
byteArray[i] = byteList.get(i);
|
}
|
String hex = bytesToHex(byteArray);
|
return Integer.parseInt(hex, 16);
|
}
|
|
|
|
public static int getBcdToInt(byte data) {
|
return ((data & 0xF0) >> 4) * 10 + ((data & 0x0F));
|
}
|
|
|
public static byte[] getIntToBCD(int number) {
|
// 获取整数的字符串表示形式
|
String numberStr = Integer.toString(number);
|
|
// 创建一个字节数组以存储BCD表示
|
int len = numberStr.length();
|
byte[] bcd = new byte[(len + 1) / 2];
|
|
int j = 0;
|
// 如果数字的长度是奇数,需要在前面添加一个0
|
if (len % 2 != 0) {
|
numberStr = "0" + numberStr; // 先添加前导零
|
len++;
|
}
|
|
// 将每一对数字转换为一个字节
|
for (int i = 0; i < len; i += 2) {
|
bcd[j++] = (byte) (((numberStr.charAt(i) - '0') << 4) | (numberStr.charAt(i + 1) - '0'));
|
}
|
|
return bcd;
|
}
|
/**
|
* 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;
|
}
|
|
/**
|
* 将byte数组转换为带符号的32位浮点数
|
* <p>
|
* 低位在前高位在后
|
*
|
* @param value
|
* @return
|
*/
|
public static Float bytesToFloat(byte[] value) {
|
ByteBuffer bufferLittleEndian = ByteBuffer.wrap(value);
|
bufferLittleEndian.order(ByteOrder.LITTLE_ENDIAN);
|
return bufferLittleEndian.getFloat();
|
}
|
|
|
/**
|
* 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;
|
}
|
|
|
/**
|
* 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();
|
}
|
|
|
/**
|
* 将 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));
|
}
|
|
|
/**
|
* float转Hex低位在前高位在后
|
*
|
* @param value
|
* @return
|
*/
|
public static String floatToHexLowHigh(Float value) {
|
String hexString = Integer.toHexString(Float.floatToIntBits(value));
|
hexString = spaceHex(hexString);
|
hexString = HighLowHex(hexString);
|
return hexString;
|
}
|
|
|
public static float hexToFloatLowHigh(byte[] data) {
|
List<Byte> byteList = new ArrayList<>();
|
for (byte b : data) {
|
byteList.add(b);
|
}
|
Collections.reverse(byteList);
|
byte[] byteArray = new byte[byteList.size()];
|
for (int i = 0; i < byteList.size(); i++) {
|
byteArray[i] = byteList.get(i);
|
}
|
String hex = bytesToHex(byteArray);
|
int intValue = Integer.parseInt(hex, 16); // 将十六进制字符串转换为整数表示
|
float floatValue = Float.intBitsToFloat(intValue); // 将整数表示转换为浮点数
|
return floatValue;
|
}
|
|
/**
|
* 十进制转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;
|
}
|
}
|
|
/**
|
* 十进制转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进制字符串
|
*
|
* @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) {
|
if (str.length() % 2 != 0) {
|
//奇数时前面加0补齐称为偶数
|
str = "0" + 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
|
*/
|
public static String HighLowHex(String str) {
|
if (str.trim().length() <= 2) return str;
|
List<String> 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;
|
}
|
}
|