-1

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

Gonzo
  • 21
  • 5
  • 1
    In what radix? Binary? Decimal? Octal? Hex? Base64? Packed decimal? Zoned decimal? – user207421 Nov 02 '11 at 23:53
  • And how do you get a 7-byte integer value in Java? – Hot Licks Nov 02 '11 at 23:54
  • @EJP In Decimal. Basicly I have to send a value that containt 15 digits, and it has to by 7 byte value – Gonzo Nov 02 '11 at 23:55
  • @Gonzo It must be *packed* decimal: two digits per byte. You are going to have to sort out your requirement first. Then, if it is packed-decimal, you are going to have to tell us *which* packed-decimal format you are using: unsigned, sign leading, sign trailing. Then tell us how this value is presently represented in your Java code. – user207421 Nov 03 '11 at 00:16
  • Convert to long and truncate the high byte. If it needs to be displayable characters then something like Base64 would be required, after converting to long first. – Hot Licks Nov 03 '11 at 00:17
  • @EJP -- Packed decimal requires 1/2 byte per digit. 15 digits would require 7.5 bytes. Packed decimal can't be used directly if it must fit into 7 bytes. – Hot Licks Nov 03 '11 at 00:18
  • Actually, Base64 won't work. 15 decimal digits is 13 hex digits and would require 19 characters in Base64. So I don't think there's any scheme that would work with printable characters. – Hot Licks Nov 03 '11 at 00:33
  • @Daniel R Hicks then the requirement isn't implementable. Decimal radix, 15 digits, 7 bytes. Packed decimal comes closest to that, but there is clearly something wrong somewhere. – user207421 Nov 03 '11 at 00:40

2 Answers2

1

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.

yurib
  • 8,043
  • 3
  • 30
  • 55
  • 1
    @Gonzo As you have marked this answer is correct, decimal radix cannot have been a requirement at all, contrary to your confusing responses above. – user207421 Nov 03 '11 at 02:13
0

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 /

alephx
  • 4,908
  • 1
  • 17
  • 12