JAVA 字符串与二进制,十六进制 相互转换

时间:2023-01-11 20:42:28
public class Test {
// 将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;
}

// 将字符串转换成二进制字符串,以空格相隔
public 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;
}

// 将二进制字符串转换成Unicode字符串
public 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);
}


//将字符串变成十六进制
public String strToHexStr(String str){
char[] strChar = str.toCharArray();
String result = "";
for (int i = 0; i < strChar.length; i++) {
result += Integer.toHexString(strChar[i]) + " ";
}
return result;
}

//将十六进制字符串换成Unicode字符串
public String hexStrToStr(String hexStr){
String[] tempStr = StrToStrArray(hexStr);
char[] tempChar = new char[tempStr.length];
for (int i = 0; i < tempStr.length; i++) {
tempChar[i] = hexStrToChar(tempStr[i]);
}
return String.valueOf(tempChar);
}

// 将二进制字符串格式化成全16位带空格的Binstr
public 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] + " ");
}
return output.toString();
}

// 将全16位带空格的Binstr转化成去0前缀的带空格Binstr
public 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" + " ");
}
}
return output.toString();
}

// 二进制字串转化为boolean型数组 输入16位有空格的Binstr
public 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
public 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(' ');
return output.toString();
}

// 将二进制字符串转换为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;
}

// 将二进制字符串转换为char
public char hexStrToChar(String binStr) {
int[] temp = hexStrToIntArray(binStr);
int sum = 0;
for (int i = 0; i < temp.length; i++) {
sum += temp[temp.length - 1 - i] << (i*4);
}
System.out.println(sum);
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;
}

//将十六进制字符串装成int数组
private int[] hexStrToIntArray(String hexStr){
char[] temp = hexStr.toCharArray();
int[] result = new int[temp.length];
for (int i = 0; i < temp.length; i++) {
if(temp[i] == 70 || temp[i] == 102){//f
temp[i]=15;
result[i] = temp[i];
}else if(temp[i] == 69 || temp[i] == 101){//e
temp[i]=14;
result[i] = temp[i];
}else if(temp[i] == 68 || temp[i] == 100){//d
temp[i]=13;
result[i] = temp[i];
}else if(temp[i] == 67 || temp[i] == 99){//c
temp[i]=12;
result[i] = temp[i];
}else if(temp[i] == 66 || temp[i] == 98){//b
temp[i]=11;
result[i] = temp[i];
}else if(temp[i] == 65 || temp[i] == 97){//a
temp[i]=10;
result[i] = temp[i];
}
else{

result[i] = temp[i]-48;
}
}
return result;
}

public static void main(String[] args) {
Test test = new Test();
String name = "陈星";
String binStr = test.StrToBinstr(name);
System.out.println(binStr);
System.out.println(test.BinstrToStr(binStr));

String hexStr = test.strToHexStr(name);
System.out.println("hexStr =" + hexStr);
System.out.println(test.hexStrToStr(hexStr));

System.out.println((char)38472);
System.out.println((char)26143);
System.out.println(2 << 2);
System.out.println((int)'F');
System.out.println((int)'f');
String value = "9648:661f";
System.out.println(test.hexStrToStr(value));

}

}