package com.xrkc.collection.util;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
/**
* @Description: 转换工具
* @Auther: wangjiao
* @Date: 2022-12-22 11:24
*/
public class HexUtils {
public static void main(String[] args) {
System.out.println(intToHex(258,1));
System.out.println(intToHex(9,5));
System.out.println(intToHexLittle(9,5));
System.out.println(intToHex(42330,2));
System.out.println(intToHexLittle(42330,2));
System.out.println(longToHex(23030300002L,5));
System.out.println(longToHexLittle(23030300002L,5));
}
/**
* "7dd",4,'0'==>"07DD"
*
* @param input 需要补位的字符串
* @param size 补位后的最终长度
* @param symbol 按symol补充 如'0'
* @return
*/
private static String fill(String input, int size, char symbol) {
if(input.length() > size){
System.out.println("字符超长,"+input+":"+input.length()+">"+size);
}
while (input.length() < size) {
input = symbol + input;
}
return input.toUpperCase();
}
/**
* int转hex16进制
* eg:intToHex(9,2)==>0009 intToHex(165,1)==>A5
* @param value 值
* @param byteLength 几个字节
* @return
*/
public static String intToHex(int value,int byteLength){
String hex=Integer.toHexString(value);
return fill(hex,byteLength*2,'0');
}
/**
* int转hex16进制(小端)
* eg:intToHexLittle(42330,2)==>5AA5
* @param value 值
* @param byteLength 几个字节
* @return
*/
public static String intToHexLittle(int value,int byteLength){
byte[] b = new byte[byteLength];
for (int i=0;i<byteLength;i++){
b[i] = (byte) (value >> i*8 & 0xff);
}
return bytesToHex(b);
}
/**
* long转hex16进制
* eg:longToHex(23030300002L,5)==>055CB67D62
* @param value 值
* @param byteLength 几个字节
* @return
*/
public static String longToHex(long value,int byteLength){
String hex=Long.toHexString(value);
return fill(hex,byteLength*2,'0');
}
/**
* long转hex16进制(小端)
* eg:longToHex(23030300002L,5)==>627DB65C05
* @param value 值
* @param byteLength 几个字节
* @return
*/
public static String longToHexLittle(long value,int byteLength){
byte[] b = new byte[byteLength];
for (int i=0;i<byteLength;i++){
b[i] = (byte) (value >> i*8 & 0xff);
}
return bytesToHex(b);
}
/**
* byte[]转HEX STRING
* @param bytes
* @return
*/
private static String bytesToHex(byte[] bytes){
// String hex=new BigInteger(1, bytes).toString(16);
// return fill(hex,byteLength*2,'0');
return ByteBufUtil.hexDump(bytes).toUpperCase();
}
public static byte[] hexToBytes(String hexString) {
if (!org.springframework.util.StringUtils.hasLength(hexString)) {
return null;
}
hexString = hexString.replaceAll(" ", "");
int len = hexString.length();
if(len%2!=0){
System.out.println("字符长度有误:"+len+",入参:"+hexString);
}
int index = 0;
byte[] bytes = new byte[len / 2];
while (index < len) {
String sub = hexString.substring(index, index + 2);
bytes[index/2] = (byte)Integer.parseInt(sub,16);
index += 2;
}
return bytes;
}
public static Long readByteBufToLong(ByteBuf data, int length) {
ByteBuf byteBuf=data.readBytes(length);
byte[] bytes = ByteBufUtil.getBytes(byteBuf);
return bytesToLong(bytes,length);
}
/**
* byte[] TO Long (Little)
* @param b
* @param length 几个字节
* @return
*/
private static long bytesToLongLittle(byte[] b,int length) {
long l = 0;
for (int i=0;i<length;i++){
l += ((long) b[i] & 0xFF) << i*8;
}
return l;
}
private static long bytesToLong(byte[] b,int length) {
long l = 0;
int len=length;
for (int i=0;i<length;i++){
len--;
l += ((long) b[i] & 0xFF)<< len*8;
}
return l;
}
/**
* 每2位隔一个空格
* FE0701810F202B000000017C to FE 07 01 81 0F 20 2B 00 00 00 01 7C
*
* @param str
* @return
*/
private static String splitStr(String str) {
String regex = "(.{2})";
return str.replaceAll(regex, "$1 ").trim();
}
/**
* 获取异或校验码
* @param para
* @return
*/
public static String getCheckData(String para) {
int length = para.length() / 2;
String[] dateArr = new String[length];
for (int i = 0; i < length; i++) {
dateArr[i] = para.substring(i * 2, i * 2 + 2);
}
String code = "00";
for (int i = 0; i < dateArr.length; i++) {
code = xor(code, dateArr[i]);
}
return code;
}
private static String xor(String strHex_X, String strHex_Y) {
//将x、y转成二进制形式
String anotherBinary = Integer.toBinaryString(Integer.valueOf(strHex_X, 16));
String thisBinary = Integer.toBinaryString(Integer.valueOf(strHex_Y, 16));
String result = "";
//判断是否为8位二进制,否则左补零
if (anotherBinary.length() != 8) {
for (int i = anotherBinary.length(); i < 8; i++) {
anotherBinary = "0" + anotherBinary;
}
}
if (thisBinary.length() != 8) {
for (int i = thisBinary.length(); i < 8; i++) {
thisBinary = "0" + thisBinary;
}
}
//异或运算
for (int i = 0; i < anotherBinary.length(); i++) {
//如果相同位置数相同,则补0,否则补1
if (thisBinary.charAt(i) == anotherBinary.charAt(i))
result += "0";
else {
result += "1";
}
}
return Integer.toHexString(Integer.parseInt(result, 2));
}
}
netty16进制相关转换,大小端转换
最新推荐文章于 2024-04-25 17:57:01 发布