Search code examples
javasocketsunsigned-integer

Sending Unsigned Bytes Over Socket In Java


I am attempting to send the following Byte Array to a device running C.

BYTES TO SEND:
80 3f 10 01 00 3c b2 5f 0d

BYTES RECEIVED BY MACHNINE:
00 3f 10 01 00 3c 32 5f 0d

It seems for some reason that java is turning the signed bit into a 0 which is manipulating what the C Machine is reading.

80 -> 00
b2 -> 32

Here is an example of my code:

try
{
    String response;
    Socket clientSocket = new Socket(iPAddress, port);
    DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
    BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

    byte[] bytes = new byte[] { (byte)0x80, 0x3f, 0x10, 0x01, 0x00, 0x3c, (byte) 0xb2, 0x5f, 0x0d};
    outToServer.write(bytes);

    response = inFromServer.readLine();   
    System.out.println("FROM SCU: " + response);
    clientSocket.close();
}
catch(Exception e)
{
    e.printStackTrace();
}

I am absolutely lost as to what I can do now as it seems nothing will work. I do not have access to the C Machine in order to change code.


Solution

  • For me it looks like your "C Machine" use only 7 bits.

    0x80 binrary represented is 1000 0000, and 0xB2 binary is 1011 0010. If you get 7 bits from right it is 000 0000 = 0x00 and 011 0010 = 0x32

    I hope this may help you