byte-->Hex

fuyou001 2008-03-26 01:34:05
RT:byte字节数组怎么转换成16进制!
...全文
682 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
fuyou001 2008-03-26
  • 打赏
  • 举报
回复
你们解答很好,但分有点不够,各位见谅!
hmsuccess 2008-03-26
  • 打赏
  • 举报
回复
注:byte2HexString//这个不是我写的,
hmsuccess 2008-03-26
  • 打赏
  • 举报
回复

public static byte uniteBytes(byte src0, byte src1)
{
byte _b0 = Byte.decode("0x" + new String(new byte[]{src0})).byteValue();
_b0 = (byte)(_b0 << 4);
byte _b1 = Byte.decode("0x" + new String(new byte[]{src1})).byteValue();
byte ret = (byte)(_b0 | _b1);
return ret;
}

public static byte[] HexString2Bytes(String src) //十六进制转byte
{

byte[] tmp = src.getBytes();
byte[] ret = new byte[tmp.length/2];
for(int i=0; i<ret.length; i++){
ret[i] = uniteBytes(tmp[i*2], tmp[i*2+1]);
}
return ret;
}

public static String byte2HexString(byte[] b) {
char[] hex = {'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
char[] newChar = new char[b.length * 2];
for(int i = 0; i < b.length; i++) {
newChar[2 * i] = hex[(b[i] & 0xf0) >> 4];
newChar[2 * i + 1] = hex[b[i] & 0xf];
}
return new String(newChar);
}



这是我写的,大家给提一点建议,谢谢
zyh45516 2008-03-26
  • 打赏
  • 举报
回复
明白了 谢谢楼上几位的解答
hmsuccess 2008-03-26
  • 打赏
  • 举报
回复

public static String Bytes2HexString(byte[] b) //byte转换为十六进制
{
String ret = "";
for (int i = 0; i < b.length; i++) {
String hex = Integer.toHexString(b[i]& 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
ret += hex.toUpperCase();
}
return ret;
}
nihuajie05 2008-03-26
  • 打赏
  • 举报
回复
果果真的很勤奋....
  • 打赏
  • 举报
回复
public class Test {

public static void main(String args[]) {
String str = "Hello";
byte[] b = str.getBytes();
String s = byte2HexString(b);
System.out.println(s);
System.out.println(new String(hexString2ByteArray(s)));

}

public static String byte2HexString(byte[] b) {
char[] hex = {'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
char[] newChar = new char[b.length * 2];
for(int i = 0; i < b.length; i++) {
newChar[2 * i] = hex[(b[i] & 0xf0) >> 4];
newChar[2 * i + 1] = hex[b[i] & 0xf];
}
return new String(newChar);
}

public static byte[] hexString2ByteArray(String hexString) {
if(hexString.length() % 2 != 0) {
throw new IllegalArgumentException("error");
}
char[] chars = hexString.toCharArray();
byte[] b = new byte[chars.length / 2];
for(int i = 0; i < b.length; i++) {
int high = Character.digit(chars[2 * i], 16) << 4;
int low = Character.digit(chars[2 * i + 1], 16);
b[i] = (byte)(high | low);
}
return b;
}
}
fuyou001 2008-03-26
  • 打赏
  • 举报
回复
谢谢火龙果精辟的讲解!
linmen1983 2008-03-26
  • 打赏
  • 举报
回复
谢谢火龙果~~~~~~移位这方面我一直也昏.....现在是茅塞顿开了
zyh45516 2008-03-26
  • 打赏
  • 举报
回复
请问 反过来 把16进制怎么转换成byte字节数组?
  • 打赏
  • 举报
回复
3 楼的倒数第六行错了:

“再将 01100000 左移 4 位就得到了 0110 即 0x6”应改为:

再将 01100000 移 4 位就得到了 0110 即 0x6
  • 打赏
  • 举报
回复
如果用 JDK 5 以上的版本,直接用:

String byteStr = String.format("%02X", b[i]);

就可以完成了。
  • 打赏
  • 举报
回复
hello 的 5 个 byte 是:

0x48 0x65 0x6C 0x6C 0x6F

以第一个字节:0x6C 为例:

(b[i] & 0xf0) >> 4
(0x6C & 0xf0) >> 4 可以获得高 4 位,即 0x6

01101100 0x6C
& 11110000 0xF0
------------------
01100000 0x60

再将 01100000 左移 4 位就得到了 0110 即 0x6

hex[0x6] 取数组中的索引为 6 的字符,即“6”

b[i] & 0xf
0x6C & 0xf 可以获得低 4 位,即 0xC
hex[0xC] 取数组中的索引为 12 的字符,即“C”

这样就完成一个字节的转换。
fuyou001 2008-03-26
  • 打赏
  • 举报
回复
火龙果,可以简单说下怎么转换的不?
  • 打赏
  • 举报
回复
public class Test {

public static void main(String args[]) {
String str = "Hello";
byte[] b = str.getBytes();
System.out.println(byte2HexString(b));
}

public static String byte2HexString(byte[] b) {
char[] hex = {'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
char[] newChar = new char[b.length * 2];
for(int i = 0; i < b.length; i++) {
newChar[2 * i] = hex[(b[i] & 0xf0) >> 4];
newChar[2 * i + 1] = hex[b[i] & 0xf];
}
return new String(newChar);
}
}

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧