Java將字符串轉成二進制碼
阿新 • • 發佈:2017-05-26
pub har toc clas += 轉換成 ava temp color
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; } //將二進制轉換成字符 publicchar 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將字符串轉成二進制碼