java 漢字按照指定字元集轉換為二進位編碼 並把二進位編碼轉換為對應的漢字 方法

來源:互聯網
上載者:User

標籤:字元   字串轉換   support   yar   span   stringbu   color   throw   div   

public static void main(String [] args) throws UnsupportedEncodingException {        int val = 5;        System.out.println(Integer.toBinaryString(7));        System.out.println(Integer.toBinaryString(7>>1));        System.out.println(0b111);        System.out.println(0b11);        System.out.println(Integer.toBinaryString(520));        System.out.println(0b1000001000);        String s = "我愛你";        byte [] nn = String.valueOf(s).getBytes("UTF-8");        System.out.println(0xFF);        for (int i=0; i< nn.length; i++) {            byte ss = nn[i];            System.out.print(ss);        }        System.out.println();        for (int i=0; i< nn.length; i++) {            byte ss = nn[i];            System.out.print(ss & 0xFF);        }        System.out.println();        for (int i=0; i< nn.length; i++) {            byte ss = nn[i];            System.out.print(Integer.toBinaryString((Integer)(ss & 0xFF)));        }        System.out.println();        //將二進位字串轉換為UTF-8對應的漢字        String binaryStr = "111001101000100010010001111001111000100010110001111001001011110110100000";        String [] binaryArr = new String [binaryStr.length()/8];        for (int i=0; i<binaryStr.length(); i = i + 8) {            binaryArr[i/8] = binaryStr.substring(i, i+8);            System.out.println(binaryArr[i/8]);        }        String s1 = String.valueOf(binaryArr);        System.out.println(s1);        StringBuilder strBuilder = new StringBuilder();        byte [] mm3 = new byte[binaryArr.length];        for(int i=0; i< binaryArr.length; i++) {            String m = binaryArr[i];            int nn4 = Integer.parseInt(m, 2);            byte mm = (byte)(nn4);            mm3[i] = mm;        }        System.out.print(new String(mm3, Charset.forName("UTF-8")));    }
11111731000001000520255-26-120-111-25-120-79-28-67-96230136145231136177228189160111001101000100010010001111001111000100010110001111001001011110110100000111001101000100010010001111001111000100010110001111001001011110110100000[Ljava.lang.String;@4d76f3f8我愛你

 

java 漢字按照指定字元集轉換為二進位編碼 並把二進位編碼轉換為對應的漢字 方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.