• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Maheshwari Anuj
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,

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. Another solution would also be welcomed.

Regards,
Anuj
 
James Sabre
Ranch Hand
Posts: 781
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The only time that the signed-unsigned nature of a long (or int or byte) matters is when one does arithmetic. In your position I would just use a long to hold the unsigned long and then use the shift and mask operators to extract each 8 bits into a byte.
 
Maheshwari Anuj
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for reply James.

But actually I have a backend system that is implemented in C# which supports some data of data types as unsigned long as C# supports unsigned datatypes. And that system is expecting me to send that data in 8 bytes as well. There range is 2*(9,223,372,036,854,775,807) that is twice what we have in java long data type. So to pass the actual value i think I need something bigger then long to accomodate exact value first (else long would make the value as neagtive). Do you have any suggestion now?

Regards,
Anuj
 
James Sabre
Ranch Hand
Posts: 781
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I have given you my suggestion but you seem either not to understand or to not believe me. Sending 8 bytes that represent an unsigned long is no different from sending 8 bytes that represents a signed long. One is still sending 8 bytes. The sign only matters when it comes to doing arithmetic operations and you don't say how you generate the unsigned long values in Java and what arithmetic operations you are doing. If you could elaborate on this then maybe I can help.
 
Maheshwari Anuj
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks James for your reply.

If I am getting you correctly, say for byte data type valid values are are -128 to +127, so the byte array in one byte would be same for +128(unsigned byte) and -128(signed byte) as +128 in java byte is equal to -128 in java byte datatype. Can you let me know if i got you correctly. If this is the case then writing a 8 byte array to the socket stream would not be an issue for me.

Also, I have to read data from socket stream. I am am getting unsigned long value in 8 bytes of data. I have to show the proper value to the user. Java long can hold upto "9223372036854775807". As I am getting unsigned data i can also get "9223372036854775808" which becomes "-9223372036854775808" in java long data type. Can you let me know if there is a way to directly read proper value in a BigInteger from the stream.

Regards,
Anuj
 
James Sabre
Ranch Hand
Posts: 781
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maheshwari Anuj wrote:Thanks James for your reply.

If I am getting you correctly, say for byte data type valid values are are -128 to +127, so the byte array in one byte would be same for +128(unsigned byte) and -128(signed byte) as +128 in java byte is equal to -128 in java byte datatype. Can you let me know if i got you correctly. If this is the case then writing a 8 byte array to the socket stream would not be an issue for me.

I can't be certain but you seem to now understand.

Also, I have to read data from socket stream. I am am getting unsigned long value in 8 bytes of data. I have to show the proper value to the user. Java long can hold upto "9223372036854775807". As I am getting unsigned data i can also get "9223372036854775808" which becomes "-9223372036854775808" in java long data type. Can you let me know if there is a way to directly read proper value in a BigInteger from the stream.


There is a constructor for BigInteger that takes and int and a byte array. If the int is 1 then the bytes are taken as unsigned.
 
Maheshwari Anuj
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey James.. Thanks.. It worked at the time of writing to the socket.

Do you have any idea for reading from the stream as it becomes negative number at the time of reading as i mentioned in my last post.

Regards,
Anuj
 
James Sabre
Ranch Hand
Posts: 781
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maheshwari Anuj wrote:Hey James.. Thanks.. It worked at the time of writing to the socket.

Do you have any idea for reading from the stream as it becomes negative number at the time of reading as i mentioned in my last post.

Regards,
Anuj


I thought I covered that at the end of my previous post!
 
Maheshwari Anuj
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your help James. It worked..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic