java字符串与⼆进制的相互转化
// 将Unicode字符串转换成bool型数组
public boolean[] StrToBool(String input) {
boolean[] output = Binstr16ToBool(BinstrToBinstr16(StrToBinstr(input)));
return output;
}
// 将bool型数组转换成Unicode字符串
public String BoolToStr(boolean[] input) {
String output = BinstrToStr(Binstr16ToBinstr(BoolToBinstr16(input)));
return output;
}
/
/ 将字符串转换成⼆进制字符串,以空格相隔
private String StrToBinstr(String str) {
char[] strChar = CharArray();
String result = "";
for (int i = 0; i < strChar.length; i++) {
result += BinaryString(strChar[i]) + " ";
}
return result;
}
// 将⼆进制字符串转换成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);
}
// 将⼆进制字符串格式化成全16位带空格的Binstr
private String BinstrToBinstr16(String input) {
StringBuffer output = 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] + " ");
}
String();
}
// 将全16位带空格的Binstr转化成去0前缀的带空格Binstr
private String Binstr16ToBinstr(String input) {
StringBuffer output = new StringBuffer();
String[] tempStr = StrToStrArray(input);
for (int i = 0; i < tempStr.length; i++) {
for (int j = 0; j < 16; j++) {
if (tempStr[i].charAt(j) == '1') {
output.append(tempStr[i].substring(j) + " ");
break;
}
if (j == 15 && tempStr[i].charAt(j) == '0')
output.append("0" + " ");
}
}
String();
}
// ⼆进制字串转化为boolean型数组输⼊16位有空格的Binstr
// ⼆进制字串转化为boolean型数组输⼊16位有空格的Binstr
private boolean[] Binstr16ToBool(String input) {
String[] tempStr = 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型数组转化为⼆进制字串返回带0前缀16位有空格的Binstr    private String BoolToBinstr16(boolean[] input) {
StringBuffer output = new StringBuffer();
for (int i = 0; i < input.length; i++) {
if (input[i])
output.append('1');
else
output.append('0');
if ((i + 1) % 16 == 0)
output.append(' ');
}
output.append(' ');
String();
}
// 将⼆进制字符串转换为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 = CharArray();
int[] result = new int[temp.length];
for (int i = 0; i < temp.length; i++) {
result[i] = temp[i] - 48;
}
return result;
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。