eclipse常用的編碼格式有:US-ASCII、UTF-16、UTF-8、GBK等。
貼個講的相當好的鏈接:http://www.qianxingzhem.com/post-1499.html
public class javatest { public static void main(String[] args) throws Exception{ String s = "太陽ABC"; /* * 因為eclipse默認的編碼是GBK,所以byte1和byte2輸出完全一樣 * GBK編碼是英文用一個字節表示,漢字用兩個字節 */ byte[] byte1 = s.getBytes(); for (byte b : byte1) { System.out.PRint(Integer.toHexString(b & 0Xff) + " "); } System.out.println(); byte[] byte2 = s.getBytes("GBK"); for (byte b : byte2) { System.out.print(Integer.toHexString(b & 0Xff) + " "); } System.out.println(); /* * ASCII碼的表示方法,并不會將漢字表示出來 */ byte[] byte3 = s.getBytes("US-ASCII"); for (byte b : byte3) { System.out.print(Integer.toHexString(b & 0Xff) + " "); } System.out.println(); /* * UTF-16三個字節表示漢字,兩個字節表示字母 */ byte[] byte4 = s.getBytes("UTF-16"); for (byte b : byte4) { System.out.print(Integer.toHexString(b & 0Xff) + " "); } System.out.println(); /* * UTF-16LE中兩個字節表示漢字,兩個字節表示字母 */ byte[] byte5 = s.getBytes("UTF-16LE"); for (byte b : byte5) { System.out.print(Integer.toHexString(b & 0Xff) + " "); } System.out.println(); /* * UTF-8中,三個字節表示漢字,兩個字節表示字母 */ byte[] byte6 = s.getBytes("UTF-8"); for (byte b : byte6) { System.out.print(Integer.toHexString(b & 0Xff) + " "); } System.out.println(); }}
|
新聞熱點
疑難解答