public class Strbinaryturn {//convert Unicode string to bool type array public boolean[] Strtobool (string input) {Boolea n[] Output = Binstr16tobool (BinstrToBinstr16 (STRTOBINSTR (input)); return output; }//Converts an array of type bool to a Unicode string public string booltostr (boolean[] input) {string output = Binstrtostr (Bin STR16TOBINSTR (BoolToBinstr16 (input))); return output; }//Convert the string to a binary string, separated by a space private string strtobinstr (String str) {char[] Strchar = Str.tochararray (); String result = ""; for (int i = 0; i < strchar.length; i++) {result + = Integer.tobinarystring (Strchar[i]) + ""; } return result; }//converts binary string to Unicode string private string Binstrtostr (String binstr) {string[] TempStr = Strtostrarra Y (BINSTR); char[] Tempchar = new Char[tempstr.length]; for (int i = 0; i < tempstr.length; i++) {Tempchar[i] = Binstrtochar(Tempstr[i]); } return string.valueof (Tempchar); }//Format binary string Binstr private string BinstrToBinstr16 (String input) {StringBuffer output = for 16-bit spaces New StringBuffer (); string[] TempStr = Strtostrarray (input); for (int i = 0, i < tempstr.length; i++) {for (int j = 16-tempstr[i].length (); j > 0; j--) Output.append (' 0 '); Output.append (Tempstr[i] + ""); } return output.tostring (); }//Convert all 16 bits of binstr with spaces Binstr private string Binstr16tobinstr (String input) {StringBuffer ou) to the 0 prefix Tput = new StringBuffer (); string[] TempStr = Strtostrarray (input); for (int i = 0, i < tempstr.length; i++) {for (int j = 0; J < + j + +) {if (tempstr[ I].charat (j) = = ' 1 ') {Output.append (tempstr[i].substring (j) + ""); Break } if (j = = && Tempstr[i].charat (j) = = ' 0 ') output.append ("0" + ""); }} return output.tostring (); }//binary string converted to Boolean array input 16-bit BINSTR private boolean[] Binstr16tobool (String input) {string[] tem PSTR = Strtostrarray (input); boolean[] Output = new boolean[tempstr.length * 16]; for (int i = 0, j = 0; I < input.length (); i++, J + +) if (Input.charat (i) = = ' 1 ') output[j ] = true; else if (Input.charat (i) = = ' 0 ') output[j] = false; else j--; return output; }//Boolean array converted to binary string return with 0 prefix 16 bits with a space binstr private String BoolToBinstr16 (boolean[] input) {Stringbu Ffer output = new StringBuffer (); for (int i = 0; i < input.length; i++) {if (Input[i]) output.append (' 1 '); else Output.append (' 0 '); if ((i + 1)% + = = 0) output.append ("); } output.append ("); return output.tostring (); }//converts binary string to char private 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; }//Converts the initial binary string into a string array, separated by a space of private string[] Strtostrarray (String str) {return str.split (""); }//converts binary string to int array private int[] Binstrtointarray (string binstr) {char[] temp = Binstr.tochararr Ay (); Int[] result = new Int[temp.length]; for (int i = 0; i < temp.length; i++) {result[i] = temp[i]-48; } return result; } }
Original address: http://blog.csdn.net/q394895302/article/details/50604929
The conversion between Java string and binary