3

I need to make use of an array of unsigned bytes. I need to send certain characters over the network to a server and some of these characters are greater that 127.

I have a simplified version of the code below to try and understand the concept:

int i= 160;
byte j = (byte) i;
System.out.println((byte)i);
System.out.println(j);

and this gives an output of: -96 -96

I need to print 160. As the server is expecting a byte of 160 and if it receives -96 it does not accept the value. The reason I used an int is that when I was reading how to get around the problem, I often came across the suggestion to just use an int, but I don't quite understand that, as I need my array to be of type byte.

This is the part of my code where I send the array:

public boolean send(byte[] data) {
    try {
     out.write(data); // Write the data to the outStream
     out.flush();
    } catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false; // Return false if the TCP Transmit failed 
// }
return false;
}

I would really appreciate it if some one could help me.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Zapnologica
  • 22,170
  • 44
  • 158
  • 253
  • Do you want to send binary data over a medium that's not binary safe? If that's the case, you could just use: http://en.wikipedia.org/wiki/Base64 – NullUserException Feb 25 '13 at 18:16

4 Answers4

10

There is no distinction in Java between signed and unsigned bytes. Both of the following will assign the same value to a byte:

byte i = (byte)160;
byte j = (byte)-96;

Its up to you as a developer to treat them as signed or unsigned when you print them out. The default is to print them signed, but you can force them to print unsigned by converting them to an integer in an unsigned manner.

System.out.println(i); // -96
System.out.println(0xff&i); // 160

If you want to know how bytes can represent both negative and positive numbers at the same time, read this article on two’s complement arithmetic in Java

Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
2

Sending -96 is the correct behavior. Signed and unsigned bytes are different when they're printed out, but not in the bit representation, and they should not make a difference when they are received by another server.

Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413
2

There is no System.out.println(byte). The closest is System.out.println(int). This means your byte values are converted to ints. The conversion extends the high bit, resulting in a negative number.

This following code will demonstrate my point:

byte[] data =
{
    (byte)0x01,
    (byte)0x02,
    (byte)0x7F,
    (byte)0x80
};

for (byte current : data)
{
    String output = String.format("0x%x, 0x%x", current, (int)current);
    System.out.println(output);
}

If you want to use System.out.println to see your byte values, mask off the top three bytes of the integer value, something like this:

System.out.println(((0x000000FF & (int)current);
DwB
  • 37,124
  • 11
  • 56
  • 82
1

Bytes are not signed or unsigned by themselves. They are interpreted as such when some operations are applied (say, compare to 0 to determine sign). The operations can be signed and unsigned, and in Java, only signed byte operations are available. So the code you cited is useless for the question - it sends bytes but does not do any operation. Better show the code which receives bytes. One correct and handy method is to use java.io.InputStream.read() method which returns byte as an integer in the range 0...255.

Alexei Kaigorodov
  • 13,189
  • 1
  • 21
  • 38
  • I don't have the code for the receiver / server. am coding a client to talk to it. I don't need to do any operations on it, just the = operator. i merely want to send the raw data over the network. – Zapnologica Feb 25 '13 at 20:20