Java將字串轉成二進位碼

來源:互聯網
上載者:User

標籤:pst   十六進位   color   []   blog   return   數組   public   nbsp   

Java將字串轉成二進位碼

public void toBinary(){    String str = "王雪";    char[] strChar=str.toCharArray();    String result="";    for(int i=0;i<strChar.length;i++){        result +=Integer.toBinaryString(strChar[i])+ " ";    }    System.out.println(result);}

輸出結果為:111001110001011 1001011011101010 

Java將二進位碼轉成字串

//將二進位字串轉換成int數組    public int[] BinstrToIntArray(String binStr) {               char[] temp=binStr.toCharArray();        int[] result=new int[temp.length];           for(int i=0;i<temp.length;i++) {            result[i]=temp[i]-48;        }        return result;    }        //將二進位轉換成字元     public char BinstrToChar(String binStr){         int[] temp=BinstrToIntArray(binStr);         int sum=0;         for(int i=0; i<temp.length;i++){             sum +=temp[temp.length-1-i]<<i;         }            return (char)sum;    }    public void BinstrToStr(){        String binStr = "111001110001011 1001011011101010 ";        String[] tempStr=binStr.split(" ");        char[] tempChar=new char[tempStr.length];        for(int i=0;i<tempStr.length;i++) {           tempChar[i]=BinstrToChar(tempStr[i]);        }        System.out.println(String.valueOf(tempChar));     }

根據Unicode碼錶,將二進位碼轉換成字元

1、先將二進位轉換成十六進位

111001110001011   -->0111 0011 1000 1011 不夠四位則高位補零(左邊) -->0x738b

1001011011101010  -->1001 0110 1110 1010 -->0x96ea。然後查Unicode碼錶可得對應字元

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.