byteArray <==> Hex
一些算法摘录自:
http://stackoverflow.com/questions/332079/in-java-how-do-i-convert-a-byte-array-to-a-string-of-hex-digits-while-keeping-le
http://rgagnon.com/javadetails/java-0596.html
http://stackoverflow.com/questions/140131/convert-a-string-representation-of-a-hex-dump-to-a-byte-array-using-java
发现经常需要使用这个函数,故放在blog上用于以后查看
一:字节数组转换到16进制形式的字符串
1:利用Integer的toHexString
/**
* 字节数组转换成十六进制字符串
*
* @param b 字节数组
*
* @return 十六进制字符串
*/
public static final String byte2hex(byte[] b) {
String hs = "";
String stmp = "";
for (int i = 0; i < b.length; i++) {
stmp = Integer.toHexString(b[i] & 0xFF);
if (stmp.length() == 1) {
hs += "0" + stmp;
} else {
hs += stmp;
}
}
return hs.toUpperCase();
}
2:利用BigInteger 和String.format
public static String toHex(byte[] bytes) {
BigInteger bi = new BigInteger(1, bytes);
return String.format("%0" + (bytes.length << 1) + "X", bi);
}
3:利用BigInteger
public static String toHex(byte[] bytes) {
BigInteger bi = new BigInteger(1, bytes);
String result = bi.toString(16);
if (result.length() % 2 != 0) {
return "0" + result;
}
return result.toUpperCase();
}
4.1: & 神奇的数
public static String toHex(byte[] b) throws Exception {
String result = "";
for (int i=0; i < b.length; i++) {
result +=
Integer.toString( ( b[i] & 0xff ) + 0x100, 16).substring( 1 );
}
return result.toUpperCase();
}
4.2 : + 神奇的数 (和上一个有关)
public static String toHex(byte[]bytes) {
StringBuilder sb = new StringBuilder(bytes.length*2);
for(byte b: bytes)
sb.append(Integer.toHexString(b+0x800).substring(1));
return sb.toString().toUpperCase();
}
5.1:映射表 移位 &
public static String toHex( byte [] raw ) {
String HEXES = "0123456789ABCDEF";
if ( raw == null ) {
return null;
}
final StringBuilder hex = new StringBuilder( 2 * raw.length );
for ( final byte b : raw ) {
hex.append(HEXES.charAt((b & 0xF0) >> 4))
.append(HEXES.charAt((b & 0x0F)));
}
return hex.toString();
}
5.1 :映射表 除法 取模
public static String toHex(byte[] bytes) {
char[] hexArray = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
char[] hexChars = new char[bytes.length * 2];
int v;
for ( int j = 0; j < bytes.length; j++ ) {
v = bytes[j] & 0xFF;
hexChars[j*2] = hexArray[v/16];
hexChars[j*2 + 1] = hexArray[v%16];
}
return new String(hexChars);
}
6:String.format
static String toHex(byte[] digest) {
StringBuilder sb = new StringBuilder();
for (byte b : digest) {
sb.append(String.format("%1$02X", b));
}
return sb.toString();
}
二:十六进制字符串转为字节数组,上一个的逆过程
/**
* 十六进制字符串转换成字节数组
*
* @param hex 十六进制字符串
*
* @return 字节数组
*
* @throws IllegalArgumentException
*/
public static final byte[] hex2byte(String hex) throws IllegalArgumentException {
if (hex.length() % 2 != 0) {
throw new IllegalArgumentException();
}
char[] arr = hex.toCharArray();
byte[] b = new byte[hex.length() / 2];
for (int i = 0, j = 0, l = hex.length(); i < l; i++, j++) {
String swap = "" + arr[i++] + arr[i];
int byteint = Integer.parseInt(swap, 16) & 0xFF;
b[j] = new Integer(byteint).byteValue();
}
return b;
}
新发现,其实JAVA中有提供这一函数,还可以偷窥其实现
javax.xml.bind.annotation.adapters.HexBinaryAdapter;
public byte[] unmarshal(String s) {
if(s==null) return null;
return DatatypeConverter.parseHexBinary(s);
}
public String marshal(byte[] bytes) {
if(bytes==null) return null;
return DatatypeConverter.printHexBinary(bytes);
}
其实很早以前看到一个实现更加简便的,但是忘了是什么了,有同学发现了告诉我下。