This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Thamaiyanthi Pitchaimuthu
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1337
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Post the code you wrote; it should be pretty short.
 
Thamaiyanthi Pitchaimuthu
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2984
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic