I have this kind of String in Java
String java_str = "\x00\x00\x00@\x02\t\x01\x00\******
As you can see there are mixed binary and text data. When I'm sending this string by socket via python example code
clientSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM);
clientSocket.connect(("127.0.0.1", 11111));
clientSocket.send(bin_data.encode());
I'm receiving this binary data
How can I use Java socket to send the data to the server to receive binary data not a string like this
CodePudding user response:
The string in your question is not valid Java:
String java_str = "\x00\x00\x00@\x02\t\x01\x00\...";
because \x
is not a valid Java string escape.
Your actual Java code (from the comment) is apparently this:
Socket socket = new Socket("127.0.0.1", 9091);
OutputStream socketOutputStream = socket.getOutputStream();
String s = "\\x00\\x00\\x00@\\x02\\t\\x01\\x00\\x00\\x00\\x00...";
socketOutputStream.write(s.getBytes("ASCII"));
What you have actually done there is to escape the backslashes. So the string literal actually contains literal backslashes, x
characters and so on. (Which is what you are seeing in the packet dump.)
The way to express an arbitrary Unicode codepoint (for example the NUL
or U 0000
codepoint) in a Java string literal is to use Java's Unicode escape syntax; e.g. \u0000
.
String s = "\u0000\u0000\u0000@\u0002\t\u0001\u0000\u0000...";
In this case, a better alternative would be to represent the data as a byte array; e.g.
byte[] bytes = { 0, 0, 0, '@', 2, '\t', 1, 0, ...};
It is more readable, and conceptually cleaner to express binary data as binary rather than encoding it as text and converting it to binary.
(But note that you will need to use (byte)
type casts for any codes between 0x80
and 0xff
and any non-constant expressions in the array initializer. That is because the Java byte
type is signed ...)
If you have a Java string that contains explicit (C / C ?) \xnn
sequences, you could convert that to a regular string with some custom code. It is a bit tedious, but the coding is fairly straightforward ... if you have written a lexer by hand before.
Note that the commonly used the Apache Commons StringEscapeUtils
(javadoc) class doesn't work here. StringEscapeUtils
implements de-escaping of (strict) Java string literal syntax, and Java doesn't recognize \xnn
escapes in Strings. (StringEscapeUtils
doesn't even handle \u
... which is technically correct, though unexpected.)