标签:
public static void main(String[] args) {
System.out.println(StrToBinstr("你好"));
System.out.println(new StringToBean().BinstrToStr("100111101100000 101100101111101"));
}
//将Unicode字符串转换成bool型数组
//将二进制字符串转换成Unicode字符串-------
private String BinstrToStr(String binStr) {
String[] tempStr=StrToStrArray(binStr);
char[] tempChar=new char[tempStr.length];
for(int i=0;i<tempStr.length;i++) {
tempChar[i]=BinstrToChar(tempStr[i]);
}
return String.valueOf(tempChar);
}
//将二进制字符串转换为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;
}
//将初始二进制字符串转换成字符串数组,以空格相隔----------
private String[] StrToStrArray(String str) {
return str.split(" ");
}
//将二进制字符串转换成int数组---------
private 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;
}
private static 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;
}
标签:
原文地址:http://www.cnblogs.com/aiwoqu/p/4308855.html