0

I'm trying to convert an int (max. 65535) to an two bytes array. In C I used an uint16, but Java doesn't know any unsigned values.

To convert the bytes to an array I tried to use this:

byte[] data = { (byte) ((num >> 8) & 0xff), (byte) (num & 0xff) };

But I only get: [63, -49] instead of: [63, 207], if I use 16335 as value. Is there a way to do this in Java?

I need this unsigned byte in an byte array to send it using an outputstream

ForJ9
  • 735
  • 8
  • 24
  • 3
    bytes are signed in java... (so -49 ~= 207) – njzk2 Oct 27 '15 at 17:28
  • Thanks, what is the best way to convert it to unsigned? Should I simply calculate 256-49 for the second value? – ForJ9 Oct 27 '15 at 17:33
  • 3
    These are the correct bytes. The question is whether you use them in any context where the sign is a problem, and if so, what you would expect there to be in place of the -49, given that 207 is not a valid value for a byte in Java. There are no unsigned integers in Java, though you can do unsigned operations using signed integers/shorts/bytes in particular cases. – RealSkeptic Oct 27 '15 at 17:33
  • I can understand why you expect 207 (wich is the unsigned equivalent of -49), but I don't understand why you expect 62 instead of 63. To print -49 as 207, use (byte & 0xFF) – JB Nizet Oct 27 '15 at 17:38
  • To convert a byte to an int in the range 0 to 255 do `int x = b & 0xFF;`. – Paul Boddington Oct 27 '15 at 17:38
  • Yes the sign is a problem :) Because I need this value in C. Is there a way to implements unsigned bytes in java? Or is there a kind of workaround? – ForJ9 Oct 27 '15 at 17:39
  • As already said: `byte & 0xFF`, or, in Java 8: `Byte.toUnsignedInt(byte)` – JB Nizet Oct 27 '15 at 17:41
  • @Paul Boddington Yes this will be an option, but I need to use this value in an byte array, to send it using an outputstream, which only supports byte array – ForJ9 Oct 27 '15 at 17:42
  • 6
    -49 is the value printed on the screen when you print the byte, because Java considers it as a signed value. But the bits inside this byte are the same as the ones that would be inside an unsigned byte 207, if unsigned bytes existed in Java. -49 is the right value. You can print it as 207 if you desire so. If you send it in the output stream and read it as an unsigned byte in C, you'll get 207. – JB Nizet Oct 27 '15 at 17:43
  • It's slightly annoying that Java's byte is signed, but it's not an actual limitation. If you use an `OutputStream` with the array `[1, -49]` and recover the same bits in C, you will get the array of unsigned bytes `[1, 207]`. In other words, if you ignore this, it should just work. – Paul Boddington Oct 27 '15 at 17:49
  • 1
    @ForJ9 they are the same thing, just send away in your byte stream. – njzk2 Oct 27 '15 at 17:59
  • the fact that bytes are signed is only a matter of representation. `-49` is the same byte as `207`, simply interpreted differently. Your outputstream does not care, just pass it -49, it'll work. – njzk2 Oct 27 '15 at 18:00
  • Can we close this as duplicate if he still insists he needs an unsigned value? ;) http://stackoverflow.com/questions/4266756/can-we-make-unsigned-byte-in-java – Kayaman Oct 27 '15 at 18:04
  • @ForJ9, if you put the signed byte in an `OutputStream` it will generate _exactly the same thing_ as the unsigned byte 207 in C. _You do not actually have a problem. Write the signed byte. It will work._ – Louis Wasserman Oct 27 '15 at 18:26
  • 1
    Thanks for all your comments! The not existing problem is now solved ;) – ForJ9 Oct 27 '15 at 18:48

1 Answers1

0

You can use java's NIO's ByteBuffer for the purpose:

byte[] bytes = ByteBuffer.allocate(4).putInt(1695609641).array();

for (byte b : bytes) {
    System.out.println(Byte.toUnsignedInt(b));
}

Hope now it would work ;)

  • No I can't, because it gives the same signed result. I need an unsigned value (e.g 207) – ForJ9 Oct 27 '15 at 17:39