|
本帖最后由 scorpio1102 于 2013-9-27 14:33 编辑
网上找的64进制的实现代码如下:
byte数组转换为64进制char数组
public static char[] encode64Digit(byte[] bytes) {
char[] out = new char[bytes.length << 1];
for (int i = 0, j = 0; i < bytes.length; i++) {
out[j++] = digits64[(0xC0 & bytes) >>> 6];
out[j++] = digits64[0x3F & bytes];
}
return out;
}
64进制char数组转换为byte数组
public static byte[] decode64Digit(char[] chars) {
if ((0x01 & chars.length) != 0) {
return null;
}
byte[] out = new byte[chars.length >> 1];
for (int i = 0, j = 0; j < chars.length; i++) {
int a = (getSize(chars[j]) << 6);
j++;
int b = getSize(chars[j]) & 0xFF;
a = (a | getSize(chars[j]));
j++;
out = (byte) (a & 0xFF);
}
return out;
}
以下Apache的commons-codec项目中针对16进制转换的方法代码如下:
public static char[] encodeHex(byte[] data) {
int l = data.length;
char[] out = new char[l << 1];
// two characters form the hex value.
for (int i = 0, j = 0; i < l; i++) {
out[j++] = DIGITS[(0xF0 & data) >>> 4 ];
out[j++] = DIGITS[ 0x0F & data ];
}
return out;
} |
|