我有一个像这样用字节写的文件
\r\x00\x00\x00\xd0{"a": "test"}
它有以下字节
[13, 0, 0, 0, -48, 123, 34, 97, 34, 58, 32, 34, 116, 101, 115, 116, 34, 125]
当这个文件被读入 Java 时,我得到了所有的转义
\\r\\x00\\x00\\x00\\xd0{"a": "test"}
当我对这个字符串执行 .getBytes()
时,我得到了
[92, 114, 92, 120, 48, 48, 92, 120, 48, 48, 92, 120, 48, 48, 92, 120, 100, 48, 123, 34, 97, 34, 58, 32, 34, 116, 101, 115, 116, 34, 125]
我必须将字符串转换为有效字节,不幸的是我无法更改文件的读取方式。我知道在 Python 中,您可以使用 'rb'
模式打开文件,一切顺利。如果java有那个能力,我就用不上了。
那么简而言之,如何将 Java 读取的字符串转换为写入文件的原始字节数组?
很抱歉,如果这个问题很简单,但我对 Java 还是很陌生。
编辑:所以我相信我的问题不同于提议的“重复问题”链接。它不是获取 java 字符串中的每个文字值并将其转换回字节。 java中的字符串已经被读者转义了。 \x00
现在是 \\x00
这不是同一个字节值。所以我想我需要一些方法来取消转义字符串?
在十六进制编辑器中查看的文件
0000000: 5c72 5c78 3030 5c78 3030 5c78 3030 5c78 \r\x00\x00\x00\x
0000010: 6430 7b22 6122 3a20 2274 6573 7422 7d0a d0{"a": "test"}.
java在十六进制编辑器中查看的字符串
0000000: 5c5c 725c 5c78 3030 5c5c 7830 305c 5c78 \\r\\x00\\x00\\x
0000010: 3030 5c5c 7864 307b 2261 223a 2022 7465 00\\xd0{"a": "te
0000020: 7374 227d 0a st"}.
在 Java 中,您将不得不解释输入字符串以获得所需的字节值。
我编写了一个解释输入字符串的 Java 应用程序。
这是输入字符串:
\r\x00\x00\x00\xd0{"a": "test"}
结果如下:
[13, 0, 0, 0, -48, 34, 97, 34, 58, 32, 34, 116, 101, 115, 116, 34, 125]
这是代码。您可能需要稍微修改代码以处理您未在问题中提出的情况。
package com.ggl.testing;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class ConvertBytes implements Runnable {
private String fileName;
public static void main(String[] args) {
new ConvertBytes("bytes.txt").run();
}
public ConvertBytes(String fileName) {
this.fileName = fileName;
}
@Override
public void run() {
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(getClass()
.getResourceAsStream(fileName)));
String line = "";
while ((line = br.readLine()) != null) {
processLine(line);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null) {
br.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void processLine(String line) {
String[] parts = line.split("(?=\\\\)");
List<Byte> byteList = new ArrayList<Byte>();
for (int i = 0; i < parts.length; i++) {
if (parts[i].equals("")) {
continue;
} else {
byteList.addAll(getValue(parts[i]));
}
}
Byte[] bytes = byteList.toArray(new Byte[byteList.size()]);
System.out.println(Arrays.toString(bytes));
}
private List<Byte> getValue(String s) {
List<Byte> byteList = new ArrayList<Byte>();
if (s.startsWith("\\x")) {
int value = Integer.valueOf(s.substring(2, 4), 16);
if (value > 127) {
value = value - 256;
}
byteList.add(Byte.valueOf((byte) value));
if (s.length() > 4) {
byteList.addAll(getAsciiValue(s.substring(4)));
}
} else if (s.equals("\\r")) {
byteList.add(Byte.valueOf((byte) 13));
} else if (s.equals("\\t")) {
byteList.add(Byte.valueOf((byte) 9));
} else {
byteList.addAll(getAsciiValue(s));
}
return byteList;
}
private List<Byte> getAsciiValue(String s) {
List<Byte> byteList = new ArrayList<Byte>();
for (int i = 0; i < s.length(); i++) {
int value = (int) s.charAt(i);
byteList.add(Byte.valueOf((byte) value));
}
return byteList;
}
}
bytes.txt 文件必须与 Java 应用程序位于同一目录中。