I have a file that is written in bytes like this
\r\x00\x00\x00\xd0{"a": "test"}
which has the following bytes
[13, 0, 0, 0, -48, 123, 34, 97, 34, 58, 32, 34, 116, 101, 115, 116, 34, 125]
when this file is read into Java I'm getting everything escaped
\\r\\x00\\x00\\x00\\xd0{"a": "test"}
when I do a .getBytes()
on this string I get
[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]
I have to convert the string to the valid bytes, I don't have the ability to change how a file is read unfortunately. I know in Python, you open a file with the 'rb'
mode and you're good to go. If java has that ability, I can't use it.
So in short, how can I convert the string Java reads into the original byte array that was written to the file?
Sorry if this question is stupid simple, but I am so green when it comes to Java.
EDIT: So I believe my question is different than the proposed "duplicate question" link. It's not taking every literal value in the java string and converting that back to a byte. The string in java has been escaped by the reader. \x00
is now \\x00
which is not the same byte value. So I guess I need some way of unescaping the string?
The file as viewed in a hex editor
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"}.
The string that java gets viewed in a hex editor
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"}.