Java 字符串转位数组
Java String to bit array
考虑以下因素String
String hexData = "1E01";
是否有简单的实现将任何hexData
转换为基于位的字符串数组,如
String hexDataBits = "0001111000000001";
?
给你。使用内置的 parseInt 函数将十六进制字符串转换为 int 值,然后将其转换为二进制字符串。
public String hexToBinary(String hexBits) {
int intversion = Integer.parseInt(hexBits, 16);
String binaryVers = Integer.toBinaryString(intversion);
return binaryVers;
}
请注意,这不是填充。如果要pad,修改binaryVers
.
例如:
// if you're dead set on having at least 16 chars, put this before the return statement
int padding = 16 - binaryVers.length();
while (padding > 0) {
binaryVers = "0" + binaryVers;
padding--;
}
考虑以下因素String
String hexData = "1E01";
是否有简单的实现将任何hexData
转换为基于位的字符串数组,如
String hexDataBits = "0001111000000001";
?
给你。使用内置的 parseInt 函数将十六进制字符串转换为 int 值,然后将其转换为二进制字符串。
public String hexToBinary(String hexBits) {
int intversion = Integer.parseInt(hexBits, 16);
String binaryVers = Integer.toBinaryString(intversion);
return binaryVers;
}
请注意,这不是填充。如果要pad,修改binaryVers
.
例如:
// if you're dead set on having at least 16 chars, put this before the return statement
int padding = 16 - binaryVers.length();
while (padding > 0) {
binaryVers = "0" + binaryVers;
padding--;
}