I’m developing an Android 2.3.3 application with Java.
This app is an iOS code with unsigned data types port to Java.
On iOS it works with UInt16 and UInt8. In one case instead using byte[] I’m using char[].
But know I have to send that char[] as a byte[] using a DatagramPacket.
If one element of char[] is 128, how can I do to insert into byte[] and the receiver gets 128. I don’t know what happens if I do this:
char c = (char)128;
byte b = (byte)c;
Which will be b value?
128 = 10000000. b = -1 or b = 127?
How can I convert char[] to byte[] without losing any bits?
In Java
charis an unsigned 16-bit quantity. So you can directly convert youruint16tocharwithout doing anything else.For unsigned 8-bit quantity you have 2 options:
Use
byte. It also holds 8 bits. You don’t lose any bits just because it is signed. However, if you do arithmetic with it you need to remember that Java will scalebyteup automatically to anintand sign-extend it. To prevent this just always mask it like this:byte b;
int foo = 5 * (b & 0xFF);
Use
char. It is unsigned and can hold 16 bits so the 8 bits will fit in there quite nicely. To put abyteinto acharjust do this:byte b;
char c = (char)(b & 0xFF); // Mask off the low-order 8 bits
To put a
charinto abytejust do:Be aware that
bytein Java is signed, so that whenever you do arithmetic with it you need to mask the low-order 8 bits only (to prevent sign-extension). Like this:You can do all the normal bitwise operations you want with a byte without having to mask: