How can I write 7 byte Integer value to DataOutputStream, which contains 15 digits?

1.7k Views Asked by At

I have to write 7 byte Integer value to DataOutputStream, this Integer contains 15 digits. How can I do that?

2

There are 2 best solutions below

1
On BEST ANSWER

7 bytes = 56 bits
that means you can represent numbers up to 2^56 which is more than necessary for 15 digit long numbers.

just convert the number to binary and store it in those 7 bytes that you're sending.

0
On

7 bytes = 56 bits, you can use long to store 15digits integer

And convert it into bytes :

long val = ...
byte [] b = new byte[7];  
for(int i=0;i<7;i++){  
    b[7 - i] = (byte)(val >>> (i * 8));  
}  

/ writing from hand, may mess sth with indexes or shifts /