• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Bear Bibeault
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • salvin francis
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Frits Walraven
Bartenders:
  • Jj Roberts
  • Carey Brown
  • Scott Selikoff

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

 
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Sheriff
Posts: 22082
113
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 22082
113
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
There's a hole in the bucket, dear Liza, dear Liza, a hole in the bucket, dear liza, a tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic