1. 二进制byte[]转换成十六进制字符串string
public String byte2hex(byte[] b) // 二行制转字符串 { String hs = ""; String stmp = ""; for (int n = 0; n < b.length; n++) { stmp = (java.lang.Integer.toHexString(b[n] & 0XFF)); if (stmp.length() == 1) hs = hs + "0" + stmp; else hs = hs + stmp; } return hs; }
|
2. 十六进制字符串string转换成二进制byte[]
public byte uniteBytes(String src0, String src1) { byte b0 = Byte.decode("0x" + src0).byteValue(); b0 = (byte) (b0 << 4); byte b1 = Byte.decode("0x" + src1).byteValue(); byte ret = (byte) (b0 | b1); return ret; }
public byte[] hex2byte(String hs) // 字符串转二行制 { int m = 0, n = 0; int l = hs.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] = uniteBytes(hs.substring(i * 2, m), hs.substring(m, n)); } return ret;
}
|
3. String转换成byte[]
4. byte[]转换成String
阅读(921) | 评论(0) | 转发(0) |