I'm trying to simulate a virtual IP datagram to send through DatagramPacket, which accepts byte array only. However, many headers in IP datagram are 16-bit long. What I'm trying to do is to divide it into 2 8-bit number, but the byte in Java is -127 to 127, what I want is 0 - 256. Is there any way to achieve it?
Asked
Active
Viewed 47 times
0
-
3Store it as a byte; convert to/from an unsigned short when you need the value. – Andy Thomas May 21 '20 at 13:20
-
https://stackoverflow.com/questions/4266756/can-we-make-unsigned-byte-in-java – Andy Thomas May 21 '20 at 13:23
-
1What exactly is the problem? Java handles arrays of bytes perfectly fine. Show example of your code where the signed value of a byte is causing trouble. – Andreas May 21 '20 at 13:24
-
*Hint:* For working with byte array data structures, you should use `ByteBuffer`. – Andreas May 21 '20 at 13:26
-
So is it okay to include a negative number in a byte array and put it in a DatagramPacket? – Chloe Sun May 23 '20 at 08:04