Calvin's Tech Space

          成于堅忍,毀于浮躁

             :: 首頁 :: 聯系 :: 聚合  :: 管理

          計算機只認識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[] = 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月)專門討論字符集(特別是轉換和編碼模式)。


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


          網站導航:
           
          主站蜘蛛池模板: 哈尔滨市| 黄陵县| 修水县| 大新县| 晋州市| 正安县| 时尚| 全椒县| 广德县| 姚安县| 安平县| 汉沽区| 伊春市| 浦江县| 上高县| 盐城市| 额尔古纳市| 利川市| 淄博市| 报价| 屏东市| 汕尾市| 阳江市| 满城县| 灵台县| 浪卡子县| 平乐县| 垣曲县| 长顺县| 呼伦贝尔市| 威信县| 屯留县| 象州县| 巨鹿县| 调兵山市| 雷山县| 吴堡县| 运城市| 喀喇沁旗| 延寿县| 信丰县|