Another topic on how to read a binary file.

P

Patricia Shanahan

Arne said:
I think you must have misread. Unless the length is supposed
to be in the hundreds of MB range.

And I find it suspiciously that it matches ASCII text.

Arne

541150531 decimal is hex 20414D43. If it were " ADC" I would have
expected to see hex 20414443, decimal 541148227.

However, 541150531 is much more likely to be ASCII " AMC" than a string
length.

If the file does have a length in the previous four bytes, I would
expect it to read out as either 4 if all is well, or a much bigger power
of two if there is a byte order issue.

Patricia
 
K

Knitter

How are you converting the 541150531 to ASCII? Maybe a silly question
but I don't know the answer...
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Patricia said:
541150531 decimal is hex 20414D43. If it were " ADC" I would have
expected to see hex 20414443, decimal 541148227.

Ooops.

I missed the D<>4 alias M<>D.

Arne
 
M

Mark Space

Knitter said:
How are you converting the 541150531 to ASCII? Maybe a silly question
but I don't know the answer...

Do you see it now? Patricia and Arne hashed it out pretty well.

541150531 = 20414D43 hex, which is the same as the sequence of bytes 20
41 4D 43 also in hex. In ASCII that's " " "A" "M" "C". Check an ASCII
table at Wikipedia or something.

Short strings, fixed length, are often used as markers and "magic
numbers" in file formats. Don't expect every string to need a length,
just the "data" ones that might actually vary...
 
K

Knitter

Thank you all for the help.
I have learning something new :) and have been able to read the damn
file. After I got past my initial problem I found how the file was
actually made. It turns out *every* string doesn't include the first
string. So those 4 bytes were the initial string that has a fixed
size.

I wasn't seeing the conversion to hex, after Mark pin-pointed it, it's
obvious :D

Thanks again, best regards,

Sergio
 
N

Nigel Wade

Knitter said:
Hi,
I know this has been asked a few zillion times but I couldn't find a
good answer for my problem.

I have a binary file, the Ant Movie Catalog database if anyone knows
the software. It is a file where information about movies is stores,
the software was created in Delphi so the binary files contains Pascal
strings and integers.

I know the file format, for example, I know that the strings are store
using a 4 bytes integer representing the string length, followed by
the actually string. What I'm falling to understand is how to read the
file.

I've been using BufferedInputStream created with a FileInputStream.
If I use the read(byte[]) method, that fills the passed array with the
array.lenght how can I transform that array of bytes into the integer
that I need?

I'm creating a simple test applications to learn how to read the
binary file. I'm starting with the header that is represented as:

strFileHeader35 = ' AMC_3.5 Ant Movie Catalog 3.5.x www.buypin.com
www.antp.be ';
OwnerName: string;
OwnerSite: string;
OwnerMail: string;
OwnerDescription: string;

So I thought of reading the 4 byte that tells me how long each string
is, convert the array with the 4 bytes into the needed integer and
then reading the string into another array with the size of the
integer I have found.

I'm stuck with how to correctly read the file, how to convert the
bytes into integers.

I'm I going the wrong way?

Thanks.

Use a ByteBuffer. After you have read a buffer of data into your app. you wrap a
ByteBuffer around it:

ByteBuffer bb = ByteBuffer.wrap(buffer);

then you can read from the ByteBuffer using its methods, .getShort(), getInt(),
getLong(), getFloat() etc.

If the data is little-endian rather than big endian then, prior to reading data
from the ByteBuffer, set the order using bb.order(ByteOrder.LITTLE_ENDIAN).

So, to read a string you could do something like (I prefer to use
DataInputStream as it has the readFully() method, removing the necessity to
loop over a read() method) :

in = new FileInputStream( file );
dataIn = new DataInputStream( in );

// read the string length
byte[] lengthBytes = new byte[4];
dataIn.readFully( lengthBytes );

// wrap the byte array in a ByteBuffer
ByteBuffer bb = ByteBuffer.wrap( lengthBytes );

// if reading little endian data
bb.order( ByteOrder.LITTLE_ENDIAN );

int stringLength = bb.getInt();
byte[] stringBytes = new byte[stringLength];
dataIn.readFully( stringBytes );

You now have the string as a byte array. You now need to convert that to a Java
String, how you do that depends on the string encoding.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top