在Java中如何将“张”转成D5C5,而不是5F20呢?
在VB中用hex(asc("张))就能转成D5C5,而在java中用Integer.toHexString得到的是5F20.

解决方案 »

  1.   

    byte[] bytes = (String.valueOf("张")).getBytes();
    int hightByte = 256 + bytes[0];
    int lowByte = 256 + bytes[1];
    int ascii = (256 * hightByte + lowByte) - 256 * 256;
    System.out.println(Integer.toHexString(ascii));
      

  2.   

    //字符转成16进制的ascii码   String str="abcdef";
       byte[] a=str.getBytes();   System.out.println(byte2hex(a));
      

  3.   

    //byte2hex方法 public static String byte2hex(byte[] b)
       {
       String hs="";
       String stmp="";
       for (int n=0;n<b.length;n++)
        {
         stmp=(java.lang.Integer.toHexString(b[n] & 0XFF));
         if (stmp.length()==1) hs=hs+"0"+stmp;
         else hs=hs+stmp;
        }
       return hs;
       }