Reputation: 689
I have a string of hex values that I am trying to write to a socket as bytes.
String confDeliv = "\\x7E\\x01\\x00\\x20\\x37\\x02\\x03\\xF2\\xD5";
I have tried doing this to try and solve my problem
byte [] Delivery_Conf = {(byte)0x7E, (byte)0x01, (byte)0x00, (byte)0x20,
(byte)0x37, (byte)0x02, (byte)0x03, (byte)0xF2, (byte)0xD5};
But I have yet to succeed to write it to the socket. I don't get any errors but when I send it to the device it doesn't do what I need it to do I have tried two different ways of doing this.
Try 1:
DataOutputStream dOut = new DataOutputStream(sock.getOutputStream()); //69.171.154.64
for (int i = 0; i < Delivery_Conf.length-1; i++) {
dOut.writeByte(Delivery_Conf[i]);
}
dOut.flush();
This method I used when I but the values into a byte array.
Try 2:
DataOutputStream dOut = new DataOutputStream(sock.getOutputStream());
dOut.writeBytes(confDeliv);
dOut.flush();
This is the method I used when I tried sending it as the string but still no luck. I am able to make the device work when I use python using its byte string.
eg.
confDel = b"\x7E\x01\x00\x20\x37\x02\x03\xF2\xD5"
I think java changes something when I send it and I think that is why I can get it to work with java. I have looked around for while but I do not seem to find anything that will help me with my problem.
Upvotes: 2
Views: 5087
Reputation: 311018
You should use the following:
byte [] Delivery_Conf = {(byte)0x7E, (byte)0x01, (byte)0x00, (byte)0x20,
(byte)0x37, (byte)0x02, (byte)0x03, (byte)0xF2, (byte)0xD5};
// ...
dos.write(Delivery_conf);
The version you had writing a byte at a time should work but it's inefficient, and it's possible that the device has timing constraints.
The version using the String
isn't correct. Adding another backslash to make \x
compile is not a correct solution: you should change \x
to \u00
throughout. Throughout the string, that is, of course.
Upvotes: 2