【发布时间】:2013-03-31 02:50:46
【问题描述】:
我最近在寻找一种将字节数组转换为十六进制字符串的编码。 一方面,我发现了以下内容:how to convert hex to byte for the following program?
我已经试过了:
StringBuffer stringbuffer = new StringBuffer(cipherbyte.length * 2);
for (int i = 0; i < cipherbyte.length; i++) {
if ((cipherbyte[i] & 0xff) < 0x10 ) {
stringbuffer.append("0");
}
stringbuffer.append(Long.toString(cipherbyte[i] & 0xff, 16));
}
String ciphertext = stringbuffer.toString();
return ciphertext;
解码:
byte[] bytes = new byte[message.length() / 2];
for (int i = 0; i < message.length(); i = i+2) {
String substr = message.substring(i, i+2);
bytes[i/2] = ((byte) Integer.parseInt(substr, 16));
}
但我不知道这些算法的详细工作原理,请有人解释一下吗?
【问题讨论】: