2

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.

5
  • Can you explain exactly what is 'wrong' with the received string on the device side? Commented Apr 8, 2016 at 6:01
  • Thats what I am trying to find out. @uniknow Commented Apr 8, 2016 at 6:03
  • When I send the bytes I need to have the '\x7E' I have to have the backwards slash or else it doesnt work how can I add that into my code? to make that work? Commented Apr 8, 2016 at 6:08
  • Have you tried to encode/decode the string you have with Hex of apache commans and see what the result is? PS: are you expecting at device side a string or an byte array? Commented Apr 8, 2016 at 6:31
  • @uniknow Anything as long as I get the full command through Commented Apr 8, 2016 at 6:36

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

6 Comments

What do you mean "change \x to \u00 throughout." I cant at that to my byte array because it mistakes it for a string.
I am speaking of 'the version using the String'. Please read what I actually wrote.
My bad. Thank you for your response.
How can I write this more efficiently?@EJP
@BSD_ You can't. There is no inefficiency here. In a real program fixed data like that would be static final of course.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.