package com.sandu.ximon.admin.utils;
|
|
import java.util.ArrayList;
|
|
/**
|
* 16
|
*/
|
public class HexStrConvertUtil {
|
/**
|
* byte数组转换二进制字符串
|
**/
|
public static String HexToByteStr(String hexStr) {
|
String[] strs = new String[]{
|
"0000","0001","0010","0011",
|
"0100","0101","0110","0111",
|
"1000","1001","1010","1011",
|
"1100","1101","1110","1111"
|
};
|
String result = "";
|
for(char c:hexStr.toCharArray()){
|
result = result + strs["0123456789ABCDEF".indexOf(c)];
|
}
|
|
return result;
|
}
|
|
/**
|
* 字符串转换为16进制字符串
|
*
|
* @param s
|
* @return
|
*/
|
public static String stringToHexString(String s) {
|
String str = "";
|
for (int i = 0; i < s.length(); i++) {
|
int ch = s.charAt(i);
|
String s4 = Integer.toHexString(ch);
|
str = str + s4;
|
}
|
return str;
|
}
|
|
/**
|
* 16进制转换成为string类型字符串
|
* @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, "UTF-8");
|
new String();
|
} catch (Exception e1) {
|
e1.printStackTrace();
|
}
|
return s;
|
}
|
|
|
/**
|
* @param: [content]
|
* @return: int
|
* @description: 十六进制转十进制
|
*/
|
// public static int HexToInt(String content){
|
// int number=0;
|
// String [] HighLetter = {"A","B","C","D","E","F"};
|
// Map<String,Integer> map = new HashMap<>();
|
// for(int i = 0;i <= 9;i++){
|
// map.put(i+"",i);
|
// }
|
// for(int j= 10;j<HighLetter.length+10;j++){
|
// map.put(HighLetter[j-10],j);
|
// }
|
// String[]str = new String[content.length()];
|
// for(int i = 0; i < str.length; i++){
|
// str[i] = content.substring(i,i+1);
|
// }
|
// for(int i = 0; i < str.length; i++){
|
// number += map.get(str[i])*Math.pow(16,str.length-1-i);
|
// }
|
// return number;
|
// }
|
|
|
/**
|
* @param bArray
|
* @return HexString
|
*/
|
public static final ArrayList bytesToHexList(byte[] bArray) {
|
StringBuffer sb = new StringBuffer(bArray.length);
|
String sTemp;
|
ArrayList arrayList = new ArrayList();
|
for (int i = 0; i < bArray.length; i++) {
|
sTemp = Integer.toHexString(0xFF & bArray[i]);
|
if (sTemp.length() < 2) {
|
arrayList.add(0 + sTemp);
|
} else {
|
arrayList.add(sTemp);
|
}
|
}
|
return arrayList;
|
}
|
|
public static final String bytesToHexString(byte[] bArray) {
|
StringBuffer sb = new StringBuffer(bArray.length);
|
String sTemp;
|
for (int i = 0; i < bArray.length; i++) {
|
sTemp = Integer.toHexString(0xFF & bArray[i]);
|
if (sTemp.length() < 2) {
|
sb.append(0);
|
}
|
sb.append(sTemp.toLowerCase());
|
}
|
return sb.toString();
|
}
|
|
public static final String byteToHexString(byte b) {
|
StringBuffer sb = new StringBuffer();
|
String sTemp;
|
sTemp = Integer.toHexString(0xFF & b);
|
if (sTemp.length() < 2) {
|
sb.append(0);
|
}
|
sb.append(sTemp.toLowerCase());
|
return sb.toString();
|
}
|
|
/**
|
* 十进制转换成十六进制
|
*/
|
public static String tenToHex(int t) {
|
return Integer.toHexString(t).toUpperCase();
|
}
|
|
/**
|
* @param src String
|
* @return byte[]
|
**/
|
public static byte[] hexStringToByt (String src) {
|
src = src.replaceAll(" ", "");
|
// "\"19 03 00 00 00 0b 07 d5\""
|
byte[] ret = new byte[src.length() / 2];
|
byte[] tmp = src.getBytes();
|
for (int i = 0; i < src.length() / 2; i++) {
|
ret[i] = uniteBytes(tmp[i * 2], tmp[i * 2 + 1]);
|
}
|
return ret;
|
}
|
|
public static byte[] hexStringToByteArray (String s) {
|
|
int len = s.length();
|
byte[] data = new byte[len/2];
|
|
for(int i = 0; i < len+1; i+=2){
|
data[i/2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) + Character.digit(s.charAt(i+1), 16));
|
}
|
|
return data;
|
}
|
|
public static byte[] hexToByteArrays(String hex) {
|
|
hex = hex.length()%2 != 0?"0"+hex:hex;
|
|
byte[] b = new byte[hex.length() / 2];
|
|
for (int i = 0; i < b.length; i++) {
|
int index = i * 2;
|
int v = Integer.parseInt(hex.substring(index, index + 2), 16);
|
b[i] = (byte) v;
|
}
|
return b;
|
}
|
|
|
|
/** 十六进制转换成字节数组 */
|
public static byte[] hexStringToBytes(String hexString) {
|
if (hexString == null || hexString.equals("")) {
|
return null;
|
}
|
hexString = hexString.toUpperCase(); // 十六进制转大写字母
|
int length = hexString.length() / 2; // 获取十六进制的长度,2个字符为一个十六进制
|
char[] hexChars = hexString.toCharArray();
|
byte[] d = new byte[length];
|
for (int i = 0; i < length; i++) {
|
int pos = i * 2;
|
d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
|
}
|
return d;
|
}
|
/** char转byte */
|
private static byte charToByte(char c) {
|
return (byte) "0123456789ABCDEF".indexOf(c);
|
}
|
// /**
|
// * 把16进制字符串转换成字节数组
|
// *
|
// * @param hex
|
// * @return
|
// */
|
// public static byte[] hexStringToByte(String hex) {
|
// int len = (hex.length() / 2);
|
// byte[] result = new byte[len];
|
// char[] achar = hex.toCharArray();
|
// for (int i = 0; i < len; i++) {
|
// int pos = i * 2;
|
// result[i] = (byte) (toByte(achar[pos]) << 4 | toByte(achar[pos + 1]));
|
// }
|
// return result;
|
// }
|
|
/**
|
* 16进制字符串 转换为对应的 byte数组
|
*/
|
public static byte[] hex2Bytes(String hex) {
|
if (hex == null || hex.length() == 0) {
|
return null;
|
}
|
|
char[] hexChars = hex.toCharArray();
|
byte[] bytes = new byte[hexChars.length / 2]; // 如果 hex 中的字符不是偶数个, 则忽略最后一个
|
|
for (int i = 0; i < bytes.length; i++) {
|
bytes[i] = (byte) Integer.parseInt("" + hexChars[i * 2] + hexChars[i * 2 + 1], 16);
|
}
|
return bytes;
|
}
|
|
|
private static byte toByte(char c) {
|
byte b = (byte) "0123456789ABCDEF".indexOf(c);
|
return b;
|
}
|
|
/**
|
* 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);
|
}
|
|
public static byte[] hexStr2Bytes(String src) {
|
int m = 0, n = 0;
|
int l = src.length() / 2;
|
System.out.println(l);
|
byte[] ret = new byte[l];
|
for (int i = 0; i < l; i++) {
|
m = i * 2 + 1;
|
n = m + 1;
|
ret[i] = Byte.decode("0x" + src.substring(i * 2, m) + src.substring(m, n));
|
}
|
return ret;
|
}
|
|
public static byte[] bytes2HexByte(byte[] b) {
|
byte[] result = new byte[b.length];
|
String hex;
|
for (int i = 0; i < b.length; i++) {
|
hex = Integer.toHexString(b[i] & 0xFF);
|
if (hex.length() == 1) {
|
hex = '0' + hex;
|
}
|
result[i] = Byte.parseByte(hex);
|
}
|
return result;
|
}
|
|
public static byte uniteBytes(byte src0, byte src1) {
|
byte _b0 = Byte.decode("0x" + new String(new byte[]{src0})).byteValue();
|
_b0 = (byte) (_b0 << 4);
|
byte _b1 = Byte.decode("0x" + new String(new byte[]{src1})).byteValue();
|
byte ret = (byte) (_b0 ^ _b1);
|
System.out.print(ret);
|
return ret;
|
}
|
|
public static boolean isHex(String str) {
|
boolean isHexFlg = true;
|
int i = 0;
|
char c;
|
for (i = 0; i < str.length(); i++) {
|
c = str.charAt(i);
|
if (!(((c >= '0') && (c <= '9')) || ((c >= 'A') && (c <= 'F')) || ((c >= 'a') && (c <= 'f')) || (c == ' '))) {
|
isHexFlg = false;
|
break;
|
}
|
}
|
return isHexFlg;
|
}
|
|
|
// public static byte[] readInputStream(InputStream inputStream) {
|
// ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
//
|
// if (inputStream != null) {
|
// try {
|
// byte[] buffer = new byte[91];
|
// int counts;
|
// while ((counts = inputStream.read(buffer, 0, buffer.length)) > 0) {
|
// byteArrayOutputStream.write(buffer, 0, counts);
|
// }
|
// } catch (IOException e) {
|
// e.printStackTrace();
|
// }/* finally {
|
// if (byteArrayOutputStream != null) {
|
// try {
|
// byteArrayOutputStream.close();
|
// } catch (IOException e) {
|
// e.printStackTrace();
|
// }
|
// }
|
// }*/
|
// }
|
// return byteArrayOutputStream.toByteArray();
|
// }
|
|
// br = new BufferedReader(new InputStreamReader(inputStream));
|
// byteArrayOutputStream = new ByteArrayOutputStream();
|
// while ((data = br.readLine()) != null) {
|
// Log.i("HexStrConvertUtil", "readInputStream: " + data);
|
// byte[] ba = HexStrConvertUtil.hexStr2Bytes(data);
|
// Log.i("HexStrConvertUtil", "bytes : " + Arrays.toString(ba));
|
// }
|
|
/**
|
* 合并数组
|
*
|
* @param firstArray 第一个数组
|
* @param secondArray 第二个数组
|
* @return 合并后的数组
|
*/
|
public static byte[] connect(byte[] firstArray, byte[] secondArray) {
|
if (firstArray == null || secondArray == null) {
|
return null;
|
}
|
byte[] bytes = new byte[firstArray.length + secondArray.length];
|
System.arraycopy(firstArray, 0, bytes, 0, firstArray.length);
|
System.arraycopy(secondArray, 0, bytes, firstArray.length, secondArray.length);
|
return bytes;
|
}
|
|
//合并数组
|
public static byte[] connectByte(byte[] firstArray, byte[] secondArray) {
|
if (firstArray == null || secondArray == null) {
|
return null;
|
}
|
byte[] bytes = new byte[firstArray.length + secondArray.length];
|
System.arraycopy(secondArray,0,bytes,firstArray.length,secondArray.length);
|
// System.arraycopy(firstArray,0,secondArray,secondArray.length,bytes.length);
|
|
// System.arraycopy(firstArray, 0, bytes, 0, firstArray.length);
|
// System.arraycopy(secondArray, 0, bytes, firstArray.length, secondArray.length);
|
return bytes;
|
}
|
}
|