This repository has been archived on 2025-09-14. You can view files and clone it, but cannot push or open issues or pull requests.
Files
nezha-olp-exporter/src/main/java/com/nis/util/HexUtils.java

208 lines
6.1 KiB
Java
Raw Normal View History

package com.nis.util;
public class HexUtils {
/**
* 16进制表示的字符串转换为字节数组
*
* @param hexString 16进制表示的字符串
* @return byte[] 字节数组
*/
public static byte[] hexStringToByteArray(String hexString) {
hexString = hexString.replaceAll(" ", "");
int len = hexString.length();
byte[] bytes = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
// 两位一组,表示一个字节,把这样表示的16进制字符串还原成一个字节
bytes[i / 2] = (byte) ((Character.digit(hexString.charAt(i), 16) << 4) + Character
.digit(hexString.charAt(i + 1), 16));
}
return bytes;
}
/**
* 字节转十六进制字符串
* @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;
}
/**
* byte[]数组转换为16进制的字符串
*
* @param bytes 要转换的字节数组
* @return 转换后的结果
*/
public static String bytesToHexString(byte[] bytes) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < bytes.length; i++) {
String hex = Integer.toHexString(0xFF & bytes[i]);
if (hex.length() == 1) {
sb.append('0');
}
sb.append(hex);
}
return sb.toString();
}
/**
* 合并字节数组System.arraycopy()方法
* @param bt1
* @param bt2
* @return
*/
public static byte[] byteMerger(byte[] bt1, byte[] bt2){
byte[] bt3 = new byte[bt1.length+bt2.length];
System.arraycopy(bt1, 0, bt3, 0, bt1.length);
System.arraycopy(bt2, 0, bt3, bt1.length, bt2.length);
return bt3;
}
/**
* 从一个byte[]数组中截取一部分
* @param src
* @param begin
* @param count
* @return
*/
public static byte[] subBytes(byte[] src, int begin, int count) {
byte[] bs = new byte[count];
for (int i=begin;i<begin+count; i++) {
bs[i-begin] = src[i];
}
return bs;
}
/**
* Hex字符串转byte :需注意的是Hex的字符串必须为十六进制的字符否则会抛出异常Hex的范围为0x00到0xFF
* @param inHex 待转换的Hex字符串 "55"
* @return 转换后的byte 85
*/
public static byte hexToByte(String inHex){
return (byte)Integer.parseInt(inHex,16);
}
/**
* 将16进制字符串转换为10进制数
* @param inHex
* @return
*/
public static int hexToInt(String inHex){
return Integer.parseInt(inHex, 16);
}
/**
* 将10进制数转换为4个字节的16进制字符串
* @param inHex
* @return
*/
public static String intToHex(int inNum){
return String.format("%08x", inNum);
}
public static String intToHex(long inNum){
return String.format("%08x", inNum);
}
public static void main(String[] args) {
// String s = " ";
// System.out.print(HexUtils.stringToHexString(s));
byte[] pzlxByte = new byte[2];
String pzlx = new String(pzlxByte);//2byte
// System.out.println(pzlx);
// System.out.println(pzlx.getBytes().length);
// System.out.println(HexUtils.stringToHexString(pzlx));
byte[] pzidByte = new byte[17];
String pzid = new String(pzidByte);//17byte
// System.out.println(pzid);
// System.out.println(pzid.getBytes().length);
// System.out.println(HexUtils.stringToHexString(pzid));
String rltHex = HexUtils.bytesToHexString(pzlx.getBytes());
String rltHex2 = HexUtils.bytesToHexString(pzid.getBytes());
System.out.println(rltHex);
System.out.println(rltHex2);
}
/**
* 16进制字符串转换为字符串
*
* @param s
* @return
*/
// public static String hexStringToString(String s) {
// if (s == null || s.equals("")) {
// return null;
// }
// s = s.replace(" ", "");
// byte[] baKeyword = new byte[s.length() / 2];
// for (int i = 0; i < baKeyword.length; i++) {
// try {
// baKeyword[i] = (byte) (0xff & Integer.parseInt(
// s.substring(i * 2, i * 2 + 2), 16));
// } catch (Exception e) {
// e.printStackTrace();
// }
// }
// try {
// s = new String(baKeyword, "gbk");
// new String();
// } catch (Exception e1) {
// e1.printStackTrace();
// }
// return s;
// }
/**
* hex字符串转byte数组:如果Hex超过0xFF显然转换后结果不是一个byte而是一个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正好为4个二进制位一个字节byte为8个二进制位因此一个字节可表示为两个十六进制数字
* 字节数组转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();
}
}