my dog learned polymorphism*
The moose likes I/O and Streams and the fly likes BigInteger to byte array of size 8 bytes (Unsigned long) in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "BigInteger to byte array of size 8 bytes (Unsigned long) in Java" Watch "BigInteger to byte array of size 8 bytes (Unsigned long) in Java" New topic
Author

BigInteger to byte array of size 8 bytes (Unsigned long) in Java

Thamaiyanthi Pitchaimuthu
Greenhorn

Joined: Aug 20, 2010
Posts: 14
Hi All,

I have a requirement where I have to convert an unsigned long into byte array of size 8 bytes. What I am planning to so is getting the number in a BigInteger and then constructing a byte array of size 8 byte out it. It is working for the range of long but above the range of long it is failing. Can anybody help me out in this.

regards..
Thamaiyanthi
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
Post the code you wrote; it should be pretty short.
Thamaiyanthi Pitchaimuthu
Greenhorn

Joined: Aug 20, 2010
Posts: 14
Hi,
This is my code,

public boolean write8bytes(byte[] bytes, int offset) {
byte temp_byte[]=ulong.toByteArray();
int array_count=temp_byte.length-1;
if (bytes.length - 8 < offset) {
return false;
}
for (int i = 7; i>=0; i--) {
if(array_count>=0)
{
bytes[offset]=temp_byte[array_count];
}
else
{
bytes[offset]=(byte)00;
}
offset++;
array_count--;
}
return true
}
the above code is to convert the big endian Biginteger into byte array in little endian unsigned 8 byte format.

byte temp_byte[] = new byte[8];
for (int i = 7; i >= 0; i--) {
temp_byte[i] = bytes[offset];
offset++;
}
ulong = new BigInteger(1, temp_byte);

this code will read the little endian format unsigned 8 byte array to Big endian Biginteger. Its working till the maximum value of long "9223372036854775807". If i increment the value its taking 9 bytes, then basically its not unsigned. but my requirement is to be unsigned is there any solution, that ll be great help full for me.
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2841
    
  11

First, remember to use code tags when posting code. That will make your code much easier to read.

Now, I think I understand your problem. When you convert the eight byte array to a BigInteger, it works right? That is, it treats it as a signed number, even if the highest order bit is 1. That's because you use the BigInteger(int, byte []) constructor and give it 1, telling it that you want the byte array to be considered a positive integer. However, internally BigInteger is storing it as two's complement, so it needs to add space for a sign bit. That's why when you ask BigInteger for a byte array, it gives you nine bytes back. The first byte will be 0x00. If you want to translate back to an eight byte little endian, signed array, you could just skip that byte.
Thamaiyanthi Pitchaimuthu
Greenhorn

Joined: Aug 20, 2010
Posts: 14
Hi Charles,

Yes hence forth i ll give tag to the codes.
That was great help for me. now i understood the logic. its is working now..
Thanks a lot..

Regards,
Thamaiyanthi
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: BigInteger to byte array of size 8 bytes (Unsigned long) in Java
 
Similar Threads
16 bit char ???
BigInteger to byte array of size 8 bytes (Unsigned long) in Java
stupid question
Expressing unsigned integers and longs
how can i convert char [] to unsigned char [] to connect to an app writen in 'C'