你的位置:首页 > Java教程

[Java教程]java Byte[] to String(hex)


 

1. 字节数组转换成16进制字符展示

2.代码

package com.goodfan;public class ByteArrayToString {  private static char[] HEX_CHAR = {'0', '1', '2', '3', '4', '5', '6', '7', '8',     '9', 'a', 'b', 'c', 'd', 'e', 'f'};    private static String byteArray2String(byte[] data){    StringBuilder stringBuilder = new StringBuilder();    for(int i=0; i<data.length; i++){      stringBuilder.append(HEX_CHAR[(data[i] & 0xf0) >>> 4]);      stringBuilder.append(HEX_CHAR[(data[i] & 0x0f)]);      if(i < data.length-1)        stringBuilder.append(" ");    }    return stringBuilder.toString();  }    public static void main(String[] args) {    byte[] data = "hello kitty".getBytes();    System.out.println(data);    String byteArray2String = byteArray2String(data);    System.out.println(byteArray2String);  }}