Java高手进来帮忙罗
来源:百度知道 编辑:UC知道 时间:2024/08/22 11:41:51
题目:
密码”“MAC码”等数据由于是二进制数据,在Java中只能以byte[]形式存放,
但往往需要转化成可见字符串在通讯报文(如XML)中传递。
例如,密文: byte[] password = {0x5A,0x08,0x00,0xFF,0xB2,0x33,0xDE,0xFF};
转化为字符串:"5A0800FFB233DEFF"
请编程实现方法byte2HexString完成这种转化:
public String byte2HexString(byte[] b)
注:字符'A'的数值为0x41,字符'0'的数值为0x30
楼主你的意思应该是char数组,楼主你想要的
public static void main(String args[]) {
StringBuffer result = new StringBuffer();
char[] a = new char[]{0x5A,0x08,0x00,0xFF,0xB2,0x33,0xDE,0xFF};
for(int i = 0; i < a.length; i ++) {
String hex = Integer.toHexString(a[i]);
hex = hex.toUpperCase();
if(hex.length() < 2) {
result.append("0" + hex);
} else {
result.append(hex);
}
}
System.out.println(result.toString());
}
public class S_5{
static byte[] password = {0x5A,0x08,0x00,(byte)0xff,(byte)0xB2,0x33,(byte)0xDE,(byte)0xFF};
public static void main(String []args) {
System.out.println(byte2HexString(password));
}
public static String byte2HexString(byte[] b){
String str = "";
for(int i = 0; i < b.length; i++){
int byte1 = b[i]&0x0f;
int byte2 = (b[i]&0xf0)>>4;
str += byte2 <= 9?""+byte2