• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to read a C++-struct-constructed Binary Data

 
Denise Advincula
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I hope you can give me ideas on how to read a binary data constructed using C++'s struct.

One record of the binary file has this C++ structure:


I was trying to extract the data using java's DataInputStream and RandomAccessFile but my results aren't what I wanted.
Any help will be highly appreciated.

Thanks in advance!
 
Rob Spoor
Sheriff
Posts: 20822
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Denise Advincula wrote:One record of the binary file has this C++ structure:

The good news: the "amount" field maps just fine to a Java double (jdouble). The bad news: there is no actual Java equivalent of unsigned long long. The long long will map to a Java long (jlong), but that will be signed. There are just two data types that can help you with that and those are BigInteger (preferred) and BigDecimal.

I was trying to extract the data using java's DataInputStream and RandomAccessFile but my results aren't what I wanted.

Apart from the problem with the unsigned long long, what are the results?
 
Denise Advincula
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello... thanks for the reply! Here's my way of extracting the data:



And here is my result:


I am not sure if what I am doing is correct but I am expecting my result to be like this:
First 8 bytes (u long long): 0000000000000000000000000000001011101110101111011011000100001001
Second 8 bytes (double): 100

Where the 0000000000000000000000000000001011101110101111011011000100001001 is the binary string of the first 8 bytes and the 100 is the double value.

I am thinking that I should convert the first 8 bytes into a binary string. That's what I'm working on now.. but if you have better ideas I hope you can share it to me. Thanks again in advance!


 
Rob Spoor
Sheriff
Posts: 20822
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use Long.toBinaryString to convert the long to a binary string, but you'll need to add your own leading zeros. As for the double, you could try using java.util.Formatter or a NumberFormat for formatting that one.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!