字符串转16进制字符
public static String strTo16(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进制字符转AscII字符串
private static String hexToAscii(String hexStr) {
StringBuilder output = new StringBuilder("");
for(int i = 0; i < hexStr.length(); i += 2) {
String str = hexStr.substring(i, i + 2);
output.append((char)Integer.parseInt(str, 16));
}
return output.toString();
}
as cII转16进制字符串
public static String asciiToHex(String asciiStr) {
char[] chars = asciiStr.toCharArray();
StringBuilder hex = new StringBuilder();
char[] var3 = chars;
int var4 = chars.length;
for(int var5 = 0; var5 < var4; ++var5) {
char ch = var3[var5];
hex.append(Integer.toHexString(ch));
}
return hex.toString();
}
16字符串进制转byte数组
public static byte[] hexStrToByteArray(String str) {
if (str == null) {
return null;
}
if (str.length() == 0) {
return new byte[0];
}
byte[] byteArray = new byte[str.length() / 2];
for (int i = 0; i < byteArray.length; i++){
String subStr = str.substring(2 * i, 2 * i + 2);
byteArray[i] = ((byte)Integer.parseInt(subStr, 16));
}
return byteArray;
}
byte数组转16进制字符串
public static String byteArrayToHexStr(byte[] byteArray) {
if (byteArray == null){
return null;
}
char[] hexArray = "0123456789ABCDEF".toCharArray();
char[] hexChars = new char[byteArray.length * 2];
for (int j = 0; j < byteArray.length; j++) {
int v = byteArray[j] & 0xFF;
hexChars[j * 2] = hexArray[v >>> 4];
hexChars[j * 2 + 1] = hexArray[v & 0x0F];
}
return new String(hexChars);
}
合并多个byte数组
//合并多个byte[]
public static byte[] byteMergerAll(byte[]... values) {
int length_byte = 0;
for (int i = 0; i < values.length; i++) {
length_byte += values[i].length;
}
byte[] all_byte = new byte[length_byte];
int countLength = 0;
for (int i = 0; i < values.length; i++) {
byte[] b = values[i];
System.arraycopy(b, 0, all_byte, countLength, b.length);
countLength += b.length;
}
return all_byte;
}
int转32位数组(占4个字节)
public static byte[] IntToUnit32Byte(int num) {
byte[] bytes = new byte[4];
bytes[3] = (byte) ((num >> 24) & 0xff);
bytes[2] = (byte) ((num >> 16) & 0xff);
bytes[1] = (byte) ((num >> 8) & 0xff);
bytes[0] = (byte) (num & 0xff);
return bytes;
}
int转16位数组(2个字节)
public static byte[] IntToUnit16Byte(int num) {
byte[] bytes = new byte[2];
bytes[0] = (byte) ((num >> 8) & 0xff);
bytes[1] = (byte) (num & 0xff);
return bytes;
}
int转6位数组(1个字节)
public static byte[] IntToUnit8Byte(int num) {
byte[] bytes = new byte[1];
bytes[0] = (byte) (num & 0xff);
return bytes;
}
int数组转8位数组
public static byte[] IntToUnit8Byte(int[] num) {
byte[] bytes = new byte[num.length];
for (int index = 0;index<bytes.length;index++){
bytes[index]=(byte) ((num[index])&0xff);
}
// bytes[0] = (byte) ((num >> 24) & 0xff);
return bytes;
}
中文转GBK内码
public static String toGBKHexString(String text) {
byte[] arrInput;
try {
char[] chars = "0123456789ABCDEF".toCharArray();
arrInput = text.getBytes("GBK");
StringBuilder sOutput = new StringBuilder(arrInput.length);
int bit;
for (int i = 0; i < arrInput.length; i++) {
bit = (arrInput[i] & 0x0f0) >> 4;
sOutput.append(chars[bit]);
bit = arrInput[i] & 0x0f;
sOutput.append(chars[bit]);
}
System.out.println(sOutput.toString());
return sOutput.toString();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return "";
}
GBK内码转中文
public static String GbkHextoText(String bytes) {
char[] chars = bytes.toCharArray();
byte[] byts = new byte[chars.length / 2];
for (int index = 0; index < chars.length; index++) {
if (index > 0 && index % 2 != 0) {
char s = chars[index];
char s0 = chars[index - 1];
int integer = Integer.valueOf(s0 + "" + s, 16);
byts[index / 2] = (byte) integer;
}
}
try {
String gbk = new String(byts, "gbk");
System.out.println(gbk);
return gbk;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return "";
}