byte tt=(byte)0xFF;我怎样System.out.println出是 FF呢?还有
byte[] tmp=new byte[]{0x01,0x02,0x03};怎样打印出是 010203呢?求解thanks

解决方案 »

  1.   

    找了半天没找到可以直接实现的。一个不算好的方法:
    public class ByteTest{
    public static String toHex1(byte num){
    String s=Integer.toHexString(num).toUpperCase();
    String s1=s.substring(s.length()-2,s.length());
    return s1;
    }
    public static String toHex2(byte num){
    String s=Integer.toHexString(num);
    String s1=s.substring(s.length()-1,s.length());
    return s1;
    }
    public static void main(String[] args){
    String s="";
    byte[] tmp=new byte[]{0x01,0x02,0x03};
    for(int i=0;i<tmp.length;i++){
    if(tmp[i]>=16)
    s+=toHex1(tmp[i]);
    else
    s+="0"+toHex2(tmp[i]);
    }
    System.out.println(s);
    }
    }