Incoming Data of bytes - java

I am dealing with a socket connection in which I send and then receive bytes. I have the socket code written and I can both send and receive bytes but there is a catch...
I am sending data within a language called Delphi and then receiving them in the android code. But I come into a bit of a problem.
In delphi a byte has a maximum value of 255 whereas in Android the maximum byte is 127. How would you get around this? Is there a way to use an unsigned byte? Should I use shorts instead of bytes?
Any help is appreciated.

To read a single byte you can cast it to a larger Java/Android primitive type to interpret it as an unsigned value by performing a bitwise & (AND) with an 8 bit mask. To write a value to the socket as an unsigned byte value use a larger primitive type to set the value, then cast this value to a byte to write to the socket. For example:
short shortVal = 255;
byte byteVal = (byte) shortVal;
System.out.println("signed value = " + byteVal);
shortVal = (short) (byteVal & 0xFF);
System.out.println("unsigned value = " + shortVal);
Java/Android interprets the byte as a signed value (in this case, -1), but the 8 bits in the byte (11111111) are still the same as the least significant 8 bits in the short 255 (0000000011111111).
The same technique can be used with larger types (use int with 16 bit mask to interpret short as unsigned, long with 32 bit mask to interpret int as unsigned), but with multibyte values be careful to take into account the endianness of the network protocol and convert byte order if needed.

Related

Is possible have byte from 0 to 255 in java?

I have system (in c#) to control user access.
I need to have the same access controller in java for an specific case.
This controller has a criptography algorithm (rijndael).
My problem is that this algorithm uses arrays of bytes (byte[]); and this byte type in c# is 0 to 255, and in java is -128 to 127. This difference generate differents results.
How can I make those two codes to use the same byte array?
PS: I can't change the c# code; if I could, I would use sbyte instead of byte.
When you read an unsigned byte[] into a signed byte[] none of the bits are harmed in this progress and no data is lost. The difference is only how the top bit is treated. If you want to turn a signed byte in a value between 0 and 255 you can & it with 0xFF e.g.
int value = bytes[i] & 0xFF;
Someone developed a library for unsigned types you might be able to use:
https://github.com/jOOQ/jOOU

How to send value bigger than 127 in byte Java

I am working on an Smart Card where there is a method in javax.smartcardio.CommandAPDU.
CommandAPDU(int cla, int ins, int p1, int p2, byte[] data, int ne)
I need to send data as byte[] (5th argument). Now my problem is that, as Java primitive data types are signed the max value of a byte can not exceed 127. I need to send a value bigger than 127. To be precise, the hex value 94 which is equal to 148.
As some solution suggests that we can cast it to integer.
byte b = -108;
int i = b & 0xff;
I can't do that as the CommandAPDU(); constructor doesn't take an []. So how to do it?
Depending on how it is interpreted by the smart card, you could just send the correct negative value. If the smart card interprets value as unsigned, you could for example send -1 for 255.
You're calculating the APDU with unsigned bytes, while Java uses signed bytes.
It's just a matter of how the data is interpreted, sending -108 to the smart card will be interpreted in exactly the same way as sending 148 from a platform using unsigned bytes. The bit combination is exactly the same.
Java can even do the conversion itself so that you can write the code using unsigned numbers;
byte data = (byte)0x94; // stores -108 in "data", which will be interpreted
// as 148 on an unsigned platform
For long blocks of data, it is probably best to use a hexadecimal encoder/decoder. But be sure that you handle the data as bytes internally (directly decode and don't look back to the hex String). The Apache codec library contains a good encoder/decoder, or you can use Bouncy Castle or Guava or use one of the many examples on SO.

Java How to read unsigned short using inputstream?

The C++ client send a byte array to the Java Server.The first two bytes indicate the length of the residual byte array.The client uses unsigned short,but there's no unsigned short in java.How can I read the first two bytes in my server?
And another problem is,in C++ the first byte indicates the lower 8 bits and the second byte indicates the upper 8 bits.For example,the two bytes is 35 02,it convert to decimal should to be 565,not 13570.
My java code like this:
DataInputStream dis = new DataInputStream(is);
int b1 = dis.readUnsignedByte();
int b2 = dis.readUnsignedByte();
int length = (b2 << 8) | b1;
It seems to work.But I can't make sure it is exact in any condition.I hope for your suggestions.thx~
Err, DataInputStream.readUnsignedShort()!
I can't make sure it is exact in any condition
You can't test 65536 values? It's not difficult.
To convert an unsigned short to a signed int you can do
int value = dis.readShort() & 0xFFFF;
Your other problem has to do with the byte order. I suggest taking a look at the ByteBuffer which you can specify the byte order on using the order method.

Sign(+/-) error in byte in Java byte setting operations

I am declearing in Java
public byte[] orbits = new byte[38];
Now if I am doing
orbits[24] = (byte)0xFF;
orbits[24] should get populated by 11111111 i.e FF(in hexadecimal) but instead its getting populated with -1.
This operation in C++ working perfectly
char orbits[38]
orbits[24] = (char)0xFF;
How to replicate the similar situation in Java using byte?
Thanks
Well, it just happens that -1 is 0xFF. Everything is correct. byte stores values from -128 to 127 using two's complement.
In Java there are no unsigned types. If you want to use bit patterns, then use byte. 0xFF and -1 are the same thing in this situation. If you want to use numbers, that is, 0xFF is actually 255 and not -1, then you need to use a bigger type, like short.

How to read negative byte values in Java less than -128

I'm reading data from another system using the serial port. I'm reading packets of 133 bytes. The second byte is the packet number and the third byte is the negative value of the packet number.
The problem is that the type byte has a range of -128 to 127. When I attempt to read -129 (outside the range of byte) it will give the value as 127.
What should I do so I can get -129?
You have to determine what range you expect byte values to have. if you expect the range -129 to 126 for example you can use.
int min = 129;
int i = ((b + min) & 0xFF) - min;
BTW You cannot have more than 256 value.
You are getting 127 because a byte is only 8 bits and so the value is wrapping around. -129 won't fit in a java byte. You will have to modify your program to use shorts at the least if you want to fit -129 in a given variable.
I have to guess here a little as I don't know the protocol.
Maybe boths values should be treated as unsigned (positive) bytes in the protocol,
they you can convert them to ints later.
// 0-255
int plus = (int)(plusByte & 0xFF);
// -255 - 0
int minus = 0 - (int)(minusByte & 0xFF);
Is it related to this Us Pat 6313763 ? But as the length of the packet is fixed, I don't get it.
It is not possible to store "bigger" numbers than the range 256 in a byte. Maybe you misunderstood the protocol, and its the high and low bits of an int stored in two bytes?
Values less than -128 dont fit into a signed byte. You need to read a short etc.

Categories