Calvin's Tech Space

          成于堅忍,毀于浮躁

             :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理

          計算機(jī)只認(rèn)識01數(shù)字,要轉(zhuǎn)換成可供閱讀的字符,需要對底層的01串按一定規(guī)則進(jìn)行解析,這個規(guī)則就是各種編碼,如ASCII,UTF-8等等。

          01串----解碼(decode)----->字符
          字符----編碼(encode)----->01串
          用什么樣的編碼方式存儲,就得用相應(yīng)的解碼方式解碼才能不出現(xiàn)亂碼。

          示例:

           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[] = 72101108112 };
           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

           

        1. "" (developerWorks,2002年10月)專門討論字符集(特別是轉(zhuǎn)換和編碼模式)。


        2. 只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 新平| 屏东县| 关岭| 安宁市| 涿州市| 湖口县| 尉氏县| 曲阜市| 梧州市| 恩施市| 荔浦县| 图片| 彰化县| 房山区| 若羌县| 岳普湖县| 永定县| 台东县| 靖西县| 峡江县| 常山县| 基隆市| 台北市| 湖口县| 东乡族自治县| 宣城市| 青铜峡市| 澎湖县| 南汇区| 蓝山县| 宜兰县| 东山县| 吉木萨尔县| 夹江县| 博客| 固镇县| 松江区| 玉山县| 新泰市| 庐江县| 泉州市|