Array of bytes in unsigned int in java

I convert an array of bytes to int by doing this:

 ByteArrayInputStream bais = new ByteArrayInputStream (data); DataInputStream dis = new DataInputStream (bais); int j = dis.readInt(); 

But it returns a signed number, where I want an unsigned number, because I want to send this number to the server as an integer during encryption and must decrypt it on the server. if it is a signed number, I can do it.

please help me..........

venu

+7
java
source share
4 answers

The explicit primitive Java types (i.e., bytes, short, int, and long) are all signed. But you can get around this.

To do (say) 32-bit unsigned arithmetic, just do the arithmetic, pretending that 'int' is unsigned. For example, 2**31 - 1 is the largest (signed) int value. If you add it to it, you will get -2**31 . But this bit pattern matches +2**31 if you think int is unsigned. It also works for subtraction and multiplication. (I'm not sure about the division and remainder, but most likely this is not important to you).

Comparing 32-bit unsigned values ​​is a bit more complicated. For example, -1 less than +1 , but if you interpret -1 as an unsigned value, you get +2**32 - 1 , which should be greater than "+1". You can compensate by translating the inequality (I'll leave it to the reader to understand this), or by casting int values ​​to long , masking them with 0xffffffffL and comparing them as longs; eg.

 int u1 = ... int u2 = ... if ((((long) u1) & 0xffffffff) < (((long) u2) & 0xffffffff) { // u1 represents a smaller unsigned value than u2 } 

Converting unsigned 32-bit integers to Strings is easiest to do with longs; eg.

 String str = Long.toString(((long) u1) & 0xffffffffL); 

Now, I will freely admit that using int to represent 32-bit unsigned values ​​is complex and potentially error prone. A cleaner solution would be to use long throughout, or if your application needs 64-bit unsigned values ​​to use BigInteger .


UPDATE - it looks like Java 8 will support (in the form of library methods) for handling int and long as unsigned types - see the "Unsigned Integer Arithmetic API is now in JDK 8" by Joseph Darcy @Oracle.

+7
source share

An int always a 32-bit number signed in Java. However, this only matters if you are doing math. If all you need is a pattern of 0 and 1 bit, just ignore the sign.

If you need to do some math, convert it to long by masking:

 long l = j & 0xFFFFFFFFL; 

Performs all arithmetic with long operands, modulo 0xFFFFFFFFL. When you are done, return the result to int and pass it.

+6
source share

Each cell in the array is treated as unsigned int:

 private int unsignedIntFromByteArray(byte[] bytes) { int res = 0; if (bytes == null) return res; for (int i = 0; i < bytes.length; i++) { res = (res *10) + ((bytes[i] & 0xff)); } return res; } 
  • Code Converts [12,34] to 1234
0
source share

There is no unsigned int in Java, so you have to manually invert the bits to get an unsigned value if you absolutely need to have one.

Got it from Google .

-one
source share

All Articles