1
2
3
4
5
6
7
8
9
|
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將二進制碼轉成字符串
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
//將二進制字符串轉換成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碼表可得對應字符
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。