成于堅忍,毀于浮躁
計算機只認識01數字,要轉換成可供閱讀的字符,需要對底層的01串按一定規則進行解析,這個規則就是各種編碼,如ASCII,UTF-8等等。 01串----解碼(decode)----->字符 字符----編碼(encode)----->01串 用什么樣的編碼方式存儲,就得用相應的解碼方式解碼才能不出現亂碼。 示例: 1public class Convert { 2 public static void main(String args[]) { 3 System.out.println(Charset.availableCharsets()); 4 Charset asciiCharset = Charset.forName("US-ASCII"); 5 CharsetDecoder decoder = asciiCharset.newDecoder(); 6 byte help[] = { 72, 101, 108, 112 }; 7 ByteBuffer asciiBytes = ByteBuffer.wrap(help); 8 CharBuffer helpChars = null; 9 try { 10 helpChars = decoder.decode(asciiBytes); 11 } catch (CharacterCodingException e) { 12 System.err.println("Error decoding"); 13 System.exit(-1); 14 } 15 System.out.println(helpChars); 16 Charset utfCharset = Charset.forName("UTF-16LE"); 17 CharsetEncoder encoder = utfCharset.newEncoder(); 18 ByteBuffer utfBytes = null; 19 try { 20 utfBytes = encoder.encode(helpChars); 21 } catch (CharacterCodingException e) { 22 System.err.println("Error encoding"); 23 System.exit(-1); 24 } 25 byte newHelp[] = utfBytes.array(); 26 for (int i = 0, n = newHelp.length; i < n; i++) { 27 System.out.println(i + " :" + newHelp[i]); 28 } 29 } 30} 31 "" (developerWorks,2002年10月)專門討論字符集(特別是轉換和編碼模式)。
計算機只認識01數字,要轉換成可供閱讀的字符,需要對底層的01串按一定規則進行解析,這個規則就是各種編碼,如ASCII,UTF-8等等。 01串----解碼(decode)----->字符 字符----編碼(encode)----->01串 用什么樣的編碼方式存儲,就得用相應的解碼方式解碼才能不出現亂碼。 示例:
Copyright @ calvin Powered by: .Text and ASP.NET Theme by: .NET Monster