现在已经写出了一个程序:import java.io.UnsupportedEncodingException;class byteToHex 

    static public String byteToHexString(byte b) {
     // Returns hex String representation of byte b        
        char hexDigit[] = {'0', '1', '2', '3', '4', '5', '6', '7',
                           '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
     
        char[] array = { hexDigit[(b >> 4) & 0x0f], hexDigit[b & 0x0f] };    
        return new String(array);
    }
 
    public static void main(String[] args)
    {
  
  String P="你好";
  byte[] bytes = null;   
try {
bytes = P.getBytes("UNICODE");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

  for(int i=0;i<bytes.length;i++){
  System.out.println(byteToHexString(bytes[i]));
  }
  
      
  }   
}我现在程序运行后的结果是
“fe
ff
4f
60
59
7d”我不明白的是“你好”的话不是应该只出现“4f  60  59  7d”么。。最开始的那个“fe ff”这两个是怎么出来的。。

解决方案 »

  1.   

    这个。。怎么可以把"fe ff"给消下去。。
    刚才试了下,如果是GBK的话就不会在开始的时候出现任何无关字符。。可是我一旦试UNICODE的话就会出现那个fe ff还麻烦大家帮忙看看了。。TVT谢谢!!
      

  2.   

    这是 Unicode 编码格式规定的,JDK 中 Unicode 是按照 UTF-16LE 进行解码。因为大家都知道二进制在网络传输时会有一个字节序的问题,而 UTF-16 编码存在字节序问题,而 UTF-8, GBK 等编码不存在这个问题。接收方为了能得知字节序,因此需要加上 BOM 头,而 Unicode 规定 U+FEFF 的字符(ZERO WIDTH NON-BREAKING SPACE)作为字节序限定字符。由于 UTF-16 存在字节序问题,因此也就有了 Big-Endian 和 Little-Endian 两种字节序方式。UTF-16LE 需要加上 BOM 头,即 FF FE,如果采用 UTF-16BE 的话是,则 BOM 为 FE FF。如果接收方收到的是 FF FE,根据 Unicode 的规定,可以立即知道这是小端序,即低位字节在前,高位字节在后。关于这个 Unicode/UTF-16LE 字节序的问题,可以参考这个帖子:
    http://topic.csdn.net/u/20081009/09/e899898c-591f-4985-ae88-5972475708fb.html关于字节序,可以参考这些文档:UTF-8, UTF-16, UTF-32 & BOM FAQ:
    http://unicode.org/faq/utf_bom.htmlRFC 2718 3.2节:
    http://www.ietf.org/rfc/rfc2781.txt
    另:你要转 Unicode 不需要这样 getByte() 解码,直接使用字符的 int 值就可以了。
      

  3.   

    public class UnicodeCode {    private final static char[] HEX = "0123456789abcdef".toCharArray();    public static void main(String[] args) {
            System.out.println(toUnicodeByte("你好"));
        }    public static String toUnicodeByte(String str) {
            if(str == null || str.length() == 0) {
                return str;
            }
            char[] chs = str.toCharArray();
            char[] bytes = new char[chs.length * 5 + 1];
            for(int i = 0, k = 0; i < chs.length; i++) {
                if(k > 0) {
                    bytes[k++] = ' ';
                }
                bytes[k++] = HEX[(chs[i] >>> 12) & 0xf];
                bytes[k++] = HEX[(chs[i] >>> 8) & 0xf];
                bytes[k++] = ' ';
                bytes[k++] = HEX[(chs[i] >>>  4) & 0xf];
                bytes[k++] = HEX[chs[i] & 0xf];
            }
            return new String(bytes);
        }
    }
      

  4.   

    char[] bytes = new char[chs.length * 5 + 1];改为:char[] bytes = new char[chs.length * 6 - 1];
      

  5.   

    public static String getUnicodeStr(String s) {
    StringBuffer buf = new StringBuffer();
    char[] cArray = s.toCharArray();
    for (char c : cArray) {
    buf.append("\\u");
    buf.append(Integer.toHexString(c));
    }
    return buf.toString();
    }