Reading Binary Data

back to main page

 

 

I just completed one of the projects, where binary file have to be parsed.
Thanks for support by Andrew Kuchukov from PDC all my problems were resolved.
I want to share this achievement with others - maybe you could face identical problem.

The task was to Parse a file with the structure

Item

Length

Type

Content

CODE

1

Byte

Version

DocNameLength

1

Byte

Length of Document Name

DocName

n

Char

Doc Name (note - not null termintated)

Reserved

4

LongInt

 

TimeStamp

4

Date/Time

As is

FileType

1

Byte

 

Length

4

LongInt

Length of the document

Document

n

Char

Document (not null terminated)

It is actually easy to use 

readblock (LENGTH1,NAME_BLOCK),
term_bin
(STRING,FILENAME,NAME_BLOCK),

However there was a land mine - STRING IS NOT NULL-TERMINATED and term_bin returns quite random results.

The solution for that was - read binary block, create binary block from just read block with one byte more and setup last byte to 0. To compose Binary you need to pass a pointer to the memory block - so, I used CAST for that operation.

readblock (DOC_BODY_LENGTH,DOC_BODY_BLOCK),
DOC_BODY_LENGTH1 = DOC_BODY_LENGTH + 1,
POINTER = cast(STRING, DOC_BODY_BLOCK),
DOC_BODY_BLOCK1=composebinary(POINTER,DOC_BODY_LENGTH1),
setbyteentry
(DOC_BODY_BLOCK1,DOC_BODY_LENGTH,0),
term_bin
(STRING,DOCBODY,DOC_BODY_BLOCK1),

Hope this code will be handy for others.

 


Copyright 1998-2000 EDMGROUP (Australia)

Last Updated: March 20, 2002