I wrote up a little example.
This is the library that I use to manipulate bytes.
@Test
public void readUnsignedInt () {
//0x53, 0x2D, 0x78, 0xAA.
//http://stackoverflow.com/questions/19874527/conversion-from-bytes-to-large-unsigned-integer-and-string
ByteBuf buf = ByteBuf.create ( 20 );
buf.addByte ( 0xAA );
buf.addByte ( 0x78 );
buf.addByte ( 0x2D );
buf.addByte ( 0x53 );
byte[] bytes = buf.readForRecycle ();
ByteBuf is a lightweight reusable buffer.
Boon is a library for implementing slice notation in Java and other utilities.
You can read unsigned bytes with idxUnsignedInt, the second arg is the offset.
long val = idxUnsignedInt ( bytes, 0 );
boolean ok = true;
BTW die throws a runtime exception and returns a boolean so you can short circuit or it to expressions to create a type of assert that can't be turned off by the compiler. :)
ok |= val == 2860002643L || die(); //die if not equal to 2860002643L
You can also read longs (you did not ask but I wanted to show you anyway).
buf.add ( 2860002643L );
bytes = buf.readForRecycle ();
val = idxLong ( bytes, 0 );
ok |= val == 2860002643L || die();
You can also add unsigned ints to a byte array buffer. Good for testing.
//add unsigned int to the byte buffer.
buf.addUnsignedInt ( 2860002643L );
//read the byte array of the buffer
bytes = buf.readForRecycle ();
//Read the unsigned int from the array, 2nd arg is offset
val = idxUnsignedInt ( bytes, 0 );
//Convert it to string and print it to console
puts("" + val);
ok |= val == 2860002643L || die();
The above covers all parts of your question. It reads it and converts it to a string.
Here is is converted to a string again.
ok |= ("" + val).equals("2860002643") || die();
Now for just a few more combinations.
//Read the unsigned int from the array, 2nd arg is offset
byte [] bytes2 = new byte[] {
(byte)0xAA, 0x78, 0x2D, 0x53, 0,
0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0 , 0, 0, 0, 0 };
val = idxUnsignedInt ( bytes2, 0 );
ok |= val == 2860002643L || die();
//Deal direct with bytes
byte [] bytes3 = new byte[20];
unsignedIntTo ( bytes3, 0, 2860002643L);
val = idxUnsignedInt ( bytes2, 0 );
ok |= val == 2860002643L || die();
}
I can never remember exactly how to do this, and I got sick of looking it up so I wrote this stuff.
You can read more about ByteBuf here. :)
https://github.com/RichardHightower/boon/wiki/Auto-Growable-Byte-Buffer-like-a-ByteBuilder