Java將字符串轉(zhuǎn)成二進(jìn)制碼,具體內(nèi)容如下
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); } |
輸出結(jié)果為:111001110001011 1001011011101010
Java將二進(jìn)制碼轉(zhuǎn)成字符串
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
|
//將二進(jìn)制字符串轉(zhuǎn)換成int數(shù)組 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; } //將二進(jìn)制轉(zhuǎn)換成字符 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)); } |
根據(jù)Unicode碼表,將二進(jìn)制碼轉(zhuǎn)換成字符
1、先將二進(jìn)制轉(zhuǎn)換成十六進(jìn)制
111001110001011 -->0111 0011 1000 1011 不夠四位則高位補(bǔ)零(左邊) -->0x738b
1001011011101010 -->1001 0110 1110 1010 -->0x96ea。然后查Unicode碼表可得對應(yīng)字符
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。