packagecom.lgdz.qydevice.utils;
importcn.hutool.core.codec.Base64;
importcn.hutool.core.util.CharsetUtil;
importcn.hutool.core.util.HexUtil;
importsun.misc.BASE64Decoder;
public classHexstringToByteUtil {
public byte[] hexToByte(String hex) {
/***先去掉16进制字符串的空格*/hex = hex.replace(" ", "");
/***字节数组长度为16进制字符串长度的一半*/intbyteLength = hex.length() / 2;
byte[] bytes = new byte[byteLength];
intm = 0;
intn = 0;
for(inti = 0; i < byteLength; i++) {
m = i * 2+ 1;
n = m + 1;
intintHex = Integer.decode("0x"+ hex.substring(i * 2, m) + hex.substring(m, n));
bytes[i] = Byte.valueOf((byte) intHex);
}
returnbytes;
}
/***字符串转为16进制字符串*/
public staticString str2HexStr(String str) {
char[] chars = "AA55A364002601FFD4C142313435390CAF".toCharArray();
System.out.println("chars:"+newString(chars));
System.out.println(chars[3]);
StringBuilder sb = newStringBuilder("");
byte[] bs = str.getBytes();
intbit;
for(inti = 0; i < bs.length; i++) {
bit = (bs[i] & 0x0f0) >> 4;
sb.append(chars[bit]);
bit = bs[i] & 0x0f;
sb.append(chars[bit]);
}
System.out.println(sb.toString().trim());
returnsb.toString().trim();
}
public staticString getHexstringToByteTest(){
String str = "AA";
byte[]data = new byte[1];
intaa= 0xAA;
data[0]=(byte) aa;
bytebase64_data[] = java.util.Base64.getEncoder().encode(data);
System.out.println(newString(base64_data));
String hex = HexUtil.encodeHexStr(str, CharsetUtil.CHARSET_UTF_8);
System.out.println(hex);
return null;
}
//public staticString getHexstringToByte(){
//AA5503640022000A D4 A5 41 38 38 38 38 38 39 01 53 52 AF// AA 55 03 64 00 22 00 0A D4 A5 41 38 38 38 38 38 39 01 53 52 AF正确播报报文bytedata[] = new byte[]{(byte)0xAA,0x55, (byte) 0x03,(byte) 0x64,(byte)0x00,0x22,(byte)0x00,(byte)0x0A,(byte)0xD4,(byte)0xA5,0x41,0x38,0x38,0x38,(byte)0x38,0x38,0x39,0X01,0X53,0X52,(byte)0xAF};
//[2, 8, 109, 90, 2, 36, 2, 19, 3]//
bytedata1[] = new byte[]{(byte)2,8, (byte) 109,(byte) 90,2,36,2,(byte)19,(byte)3};
String base64_data1 = Base64.encode(data1);
System.out.println("21312321===="+base64_data1);
bytebase64_data[] = java.util.Base64.getEncoder().encode(data);
String encode1 = Base64.encode(data);
String base64_str = newString(base64_data);
System.out.println("base64_str:"+base64_str);
System.out.println("encode1:"+encode1);
// String encode4 = Base64.encode( "AA55A364002601FFD4C142313435390CAF");// System.out.println(encode4);return null;
}
/***该工具是把16进制字符串转为16进制数据*@paramhex*@return*/public static byte[] hexString2Bytes(String hex) {
if((hex == null) || (hex.equals(""))){
return null;
}
else if(hex.length()%2!= 0){
return null;
}
else{
hex = hex.toUpperCase();
intlen = hex.length()/2;
byte[] b = new byte[len];
char[] hc = hex.toCharArray();
for(inti=0; i
intp=2*i;
b[i] = (byte) (charToByte(hc[p]) << 4| charToByte(hc[p+1]));
}
returnb;
}
}
/***该工具是把16进制字符串转为16进制,然后转为base64编码*@paramhex*@return*/public staticString hexStringToBytesToBase64(String hex) {
if((hex == null) || (hex.equals(""))){
return null;
}
else if(hex.length()%2!= 0){
return null;
}
else{
hex = hex.toUpperCase();
intlen = hex.length()/2;
byte[] b = new byte[len];
char[] hc = hex.toCharArray();
for(inti=0; i
intp=2*i;
b[i] = (byte) (charToByte(hc[p]) << 4| charToByte(hc[p+1]));
}
String encode1 = Base64.encode(b);
returnencode1;
}
}
private static bytecharToByte(charc) {
return(byte) "0123456789ABCDEF".indexOf(c);
}
public staticString decode(String s) {
if(s == null)
return null;
BASE64Decoder decoder = newBASE64Decoder();
try{
byte[] b = decoder.decodeBuffer(s);
return newString(b,"GBK");
} catch(Exception e) {
return null;
}
}
private static final char[] DIGITS= {'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
public static finalString toHex(byte[] data) {
finalStringBuffer sb = newStringBuffer(data.length* 2);
for(inti = 0; i < data.length; i++) {
sb.append(DIGITS[(data[i] >>> 4) & 0x0F]);
sb.append(DIGITS[data[i] & 0x0F]);
}
returnsb.toString();
}
public static voidmain(String[] args) {
// System.out.println(HexstringToByteUtil.str2HexStr("AA55A364002601FFD4C142313435390CAF"));
// byte b = Convert.ToByte(s, 16);用这个 对接臻识485字符串数据转base64/* byte[] aa = HexstringToByteUtil.hexString2Bytes("AA55A364002601FFD4C142313435390CAF");String encode1 = Base64.encode(aa);System.out.println(encode1);*/
//getHexstringToByte();// System.out.println( HexstringToByteUtil.decode("AghtWgIkAhMD"));
/*String guid = "AA55036400";byte[] decoded = Base64.decode(guid);System.out.println(toHex(decoded));*/
/* //16进制字符串转base64byte[] xx= HexstringToByteUtil.hexString2Bytes("AA5503640022000AD4A541383838383839015352AF");String encode1 = Base64.encode(xx);System.out.println(encode1);*///Base64转byte[],再转16进制字符串String guid = "qlUDZAAiAAnUpUNGMDAzWCiCPK8=";
byte[] decoded = java.util.Base64.getDecoder().decode(guid);
System.out.println(toHex(decoded));
}
}