Java 編碼錶__Java學習

來源:互聯網
上載者:User
package io.p7.encoding;import java.io.BufferedWriter;import java.io.UnsupportedEncodingException;public class EncodeDemo {public static void main(String[] args) throws UnsupportedEncodingException {/* * 字串--->位元組數組:編碼 * 位元組數組--->字串:解碼 *  * 你好:gbk:-60,-29,-70,-61, *  * 你好:utf-8 :-28,-67,-96,-27,-91,-67, *  * 如果你編錯了  解不出來 * 如果你編對了  解錯了有可能還有救 * (如果你對應了另一個碼錶中的文字 可以將這個文字在編碼成碼 然後在將這個碼 找到正確的碼錶 解碼 * 但如果 找錯了碼錶並且 這麼碼錶中 沒有該文字對應的編碼  則會用特殊編碼錶示該文字   該特殊碼無法在解碼成正確文字 即使找到正確碼錶) */ String str="你好";byte[]buf=str.getBytes("gbk");String s1=new String(buf,"utf-8");System.out.println("s1="+s1);byte[]buf2=s1.getBytes("utf-8");String s2=new String(buf2,"gbk");System.out.println("s2="+s2);//encodeDemo();}private static void ncodeDemo() throws UnsupportedEncodingException {String str="你好";//編碼byte[]buf=str.getBytes("utf-8");//printBytes(buf);//解碼String s1=new String(buf,"utf-8");System.out.println("s1="+s1);}private static void printBytes(byte[] buf) {for(byte b:buf) {System.out.print(b+",");}}}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.