1

I'm attempting to build a TCP Client in C# that passes over files (mainly MP3) over to an Android application, but I'm having great difficulty converting between unsigned and signed bytes.

What am I doing wrong to get the mismatch in values retrieved by the app?

The below is the data C# is sending across via TCP.

new byte[] { 9, 1, 251, 252, 253, 254, 255, 254, 253, 252, 251, }

Java

while ((charsRead = in.read(buffer)) != -1)
{
    serverMessage = new String(buffer).substring(0, charsRead);
    serverByteMessage = serverMessage.getBytes();
    for(int i = 0; i < serverByteMessage.length; i++) {
        int bi = serverByteMessage[i] & 0xFF;
        Log.e("TCP Client", "Item: " + serverByteMessage[i]);
        Log.e("TCP Client", "Value of my test unsigned byte: " + bi);
    }
}

Java Output

Item: 9
Value of my test unsigned byte: 9
Item: 1
Value of my test unsigned byte: 1
Item: -17
Value of my test unsigned byte: 239
Item: -65
Value of my test unsigned byte: 191
Item: -67
Value of my test unsigned byte: 189
Item: -17
Value of my test unsigned byte: 239
Item: -65
Value of my test unsigned byte: 191
Item: -67
Value of my test unsigned byte: 189
Item: -17
Value of my test unsigned byte: 239
Item: -65
Value of my test unsigned byte: 191
Item: -67
Value of my test unsigned byte: 189
Item: -17
Value of my test unsigned byte: 239
Item: -65
Value of my test unsigned byte: 191
Item: -67
Value of my test unsigned byte: 189
Item: -17
Value of my test unsigned byte: 239
3
  • 1
    There is no mismatch. Java's bytes are always signed. An unsigned byte with the value of 239 represents exactly the same value as a signed byte with the value of -17. You don't need to perform any conversion; functions that handle the received data as a whole (for example, decode an MP3 file) will handle your data correctly. Commented Jan 17, 2016 at 13:11
  • Thanks for your comment. If I write this byte to a file though and compare the original in a hex editor, won't these files be different? (I've not yet tested this so I'm guessing. ;-) ) Commented Jan 17, 2016 at 13:20
  • No, they will not be different. Commented Jan 17, 2016 at 13:48

1 Answer 1

1

When you convert binary to text, you can only convert valid byte encodings to text. If you convert random data to text, any invalid code are typically replaced with a ?

The simple solution is to avoid mixing text and binary unless you really know what you are doing.

InputStream in = socket.getInputStream();
int bytesRead;
byte[] bytes = new bytes[512];
while((bytesRead = in.read(bytes)) > 0) {
   for (int i =0; i < bytesread; i++) {
       int bi = bytesRead[i] & 0xFF;
       System.out.println(bi);
   }
}

Note: You cannot assume that TCP supports messages. It only supports a stream of bytes. Your have to have a protocol which allows you to work out when a message starts/finishes. e.g. you should send the length of the message before the actual message.

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

3 Comments

On first run through, this will work as the second variable (1 in my example, the 9 being the length) already is telling me whether the following bytes should be a file / text, so I can convert if need be. Reading the data as bytes rather than char makes more sense for my needs and on first test I'm getting the expected returns. (e.g. Buffer Output: 9, Buffer Output: 1, Buffer Output: 251, Buffer Output: 252 etc) Thanks!
@RichardWhitehouse This will happen provided there is enough time between messages and they are not too large. If you send messages closer together in time they will be coalesced and you will get one read with both of them. If your packets is larger, it will be broken into multiple reads. The minimum size from a single read is 1 byte.
I found out previously multiple messages can be retrieved at once, which is why I've included the length. Just split them up and deal with them. Thanks for the heads up.

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.