package com.hdxinfo.smscenter.utils;
import java.nio.ByteBuffer;
/**
* @author ztd java进制之间的转换
*/
public class TypeConvert {
/**
* String转换为16进制byte[]
*
* @param temp
* @return
package com.hdxinfo.smscenter.utils;
import java.io.ByteArrayOutputStream;
/**
* 字符串以及byte[]的普通编码和16进制编码之间的转换
* @author ztd
*
*/
public class ByteStringBetween16 {
public static void main(String[] args) {
System.out.println(encode("中文"));
System.out.println(decode(encode("中文")));
}
/**
* string -> byte[]16
*
* 第一步:把字符串转换为16进制的字符串
* 第二步:获取16进制的字符串的byte[]数组就行了
*
* @param s
* @return
*/
public static byte[] toHexStringByte(String s) {
return toHexString(s).getBytes();
}
/**
* int -> byte[] 16
*
* @param i
* @return 转换为16进制之后的byte[]数组
*/
public byte[] intTo16byte(int i) {
// int i = 0xa;
String s = Integer.toHexString(i);
byte[] b = s.getBytes();
return b;
}
/**
* 获取字符串的16进制的字符串形式
*
* @param s
* @return
*/
public static String toHexString(String s) {
String str = "";
for (int i = 0; i < s.length(); i++) {
int ch = (int) s.charAt(i);
String s4 = Integer.toHexString(ch);
str = str + s4;
}
return str;
}
/**
* 转化十六进制编码字符串为普通字符串
*
* @param s
* @return
*/
public static String toStringHex(String s) {
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");// UTF-16le:Not
} catch (Exception e1) {
e1.printStackTrace();
}
return s;
}
/*
* 16进制数字字符集
*/
private static String hexString = "0123456789ABCDEF";
/**
* 将字符串编码成16进制数字,适用于所有字符(包括中文)
*
* @param str
* @return
*/
public static String encode(String str) {
// 根据默认编码获取字节数组
byte[] bytes = str.getBytes();
StringBuilder sb = new StringBuilder(bytes.length * 2);
// 将字节数组中每个字节拆解成2位16进制整数
for (int i = 0; i < bytes.length; i++) {
sb.append(hexString.charAt((bytes[i] & 0xf0) >> 4));
sb.append(hexString.charAt((bytes[i] & 0x0f) >> 0));
}
return sb.toString();
}
/**
* 将16进制数字解码成字符串,适用于所有字符(包括中文)
*
* @param bytes
* @return
*/
public static String decode(String bytes) {
ByteArrayOutputStream baos = new ByteArrayOutputStream(
bytes.length() / 2);
// 将每2位16进制整数组装成一个字节
for (int i = 0; i < bytes.length(); i += 2)
baos.write((hexString.indexOf(bytes.charAt(i)) << 4 | hexString
.indexOf(bytes.charAt(i + 1))));
return new String(baos.toByteArray());
}
}
/**
* 字符串以及byte[]的普通编码和16进制编码之间的转换
* @author ztd
*
*/
public class ByteStringBetween16 {
public static void main(String[] args) {
System.out.println(encode("中文"));
System.out.println(decode(encode("中文")));
}
/**
* string -> byte[]16
*
* 第一步:把字符串转换为16进制的字符串
* 第二步:获取16进制的字符串的byte[]数组就行了
*
* @param s
* @return
*/
public static byte[] toHexStringByte(String s) {
return toHexString(s).getBytes();
}
/**
* int -> byte[] 16
*
* @param i
* @return 转换为16进制之后的byte[]数组
*/
public byte[] intTo16byte(int i) {
// int i = 0xa;
String s = Integer.toHexString(i);
byte[] b = s.getBytes();
return b;
}
/**
* 获取字符串的16进制的字符串形式
*
* @param s
* @return
*/
public static String toHexString(String s) {
String str = "";
for (int i = 0; i < s.length(); i++) {
int ch = (int) s.charAt(i);
String s4 = Integer.toHexString(ch);
str = str + s4;
}
return str;
}
/**
* 转化十六进制编码字符串为普通字符串
*
* @param s
* @return
*/
public static String toStringHex(String s) {
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");// UTF-16le:Not
} catch (Exception e1) {
e1.printStackTrace();
}
return s;
}
/*
* 16进制数字字符集
*/
private static String hexString = "0123456789ABCDEF";
/**
* 将字符串编码成16进制数字,适用于所有字符(包括中文)
*
* @param str
* @return
*/
public static String encode(String str) {
// 根据默认编码获取字节数组
byte[] bytes = str.getBytes();
StringBuilder sb = new StringBuilder(bytes.length * 2);
// 将字节数组中每个字节拆解成2位16进制整数
for (int i = 0; i < bytes.length; i++) {
sb.append(hexString.charAt((bytes[i] & 0xf0) >> 4));
sb.append(hexString.charAt((bytes[i] & 0x0f) >> 0));
}
return sb.toString();
}
/**
* 将16进制数字解码成字符串,适用于所有字符(包括中文)
*
* @param bytes
* @return
*/
public static String decode(String bytes) {
ByteArrayOutputStream baos = new ByteArrayOutputStream(
bytes.length() / 2);
// 将每2位16进制整数组装成一个字节
for (int i = 0; i < bytes.length(); i += 2)
baos.write((hexString.indexOf(bytes.charAt(i)) << 4 | hexString
.indexOf(bytes.charAt(i + 1))));
return new String(baos.toByteArray());
}
}