提问人:NoamiA 提问时间:10/4/2023 更新时间:10/4/2023 访问量:51
java 将二进制输入读取为 49,48
java read binary input as 49,48
问:
我收到此输入10010011 00000001 跟:
byte[] binaryInput = new byte[1024];
int bytesReadinput = System.in.read(binaryInput);
// Create a ByteArrayInputStream object for the binary input.
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(binaryInput, 0, bytesReadinput);
为什么 java 将输入获取为 49,48,48,48,49,49,32,48,48,48,48,48,48,48,49
以及如何将其更改为实际的二进制值?0x93 0x01 ?
答:
-1赞
g00se
10/4/2023
#1
您可能需要这样的东西,基于您的我从带有二进制数据的文件中得到它
public static byte[] parseBitsFile(String path) throws IOException {
String s = Files.readString(Path.of(path)).replaceAll("[^01]", "");
byte[] result = new byte[s.length() / 8];
StringReader sr = new StringReader(s);
char[] byteVal = new char[8];
int ix = 0;
while(sr.read(byteVal) > -1) {
result[ix++] = (byte)Integer.parseInt(String.valueOf(byteVal), 2);
}
return result;
}
上面假设将整个文件读入内存不是问题
评论