I am have never had to work with binary files before and am completely in the dark when it comes to reading specific data from the file and manipulating that data. I have created a generic algorithm of what I am trying to accomplish with this binary file and I would appreciate any help I could get with regard to:
1. reading specific file segments (32-bit and 64-bit values)
2. Converting binary data into the following that can then be written to an ASCII file:
a. ASCII characters AND
b. decimal values
GENERIC ALGORITHM:
1. Get Record contents and export to ASCII file
a. Get RECORD number from record header (bytes 100 – 103, inclusive) (for RECORD 1 only; RECORD 2+ will vary as described below)
i. Go to byte 100 (for RECORD 1 only; RECORD 2+ will vary as described below)
ii. Read next 4 bytes (will be the same in each RECORD HEADER)
iii. Convert 4 bytes (32-bit value) to decimal number in ASCII format
iv. Save decimal number
v. Write to ASCII file, line 1; append line with coma; do not leave line 1
b. Determine Record Length from Record header (bytes 104 – 107, inclusive)(for RECORD 1 only; RECORD 2+ will vary as described below)
i. Go to byte 104 (for RECORD 1 only; RECORD 2+ will vary as described below)
ii. Read next 4 bytes (will be the same in each RECORD HEADER)
iii. Covert 4 bytes (32-bit value) to decimal value
iv. Multiply decimal value by 2 (this will determine record content length; decimal value represent number of 16-bit entries; total record content length (excluding header) will be, at minimum, 16 bytes).
c. Read record contents (bytes 108 – value determined above)
i. Go to byte 108 (for RECORD 1 only; RECORD 2+ will vary as described below)
ii. Read next 8 bytes (64-bit value)
iii. Convert 8 bytes to decimal value (will be an unsigned floating point number)
iv. Write number to ASCII file, line 1; append coma, stay on line.
v. Go to byte 112 (for RECORD 1 only; RECORD 2+ will vary as described below)
vi. Read next 8 bytes (64-bit value)
vii. Covert 8 bytes to ASCII string (non-numeric)
viii. Write string to ASCII file, line 1
ix. Check to see if end of record (determined by length of record (1.b)
1. If not end of record, append coma to line 1, repeat 1.c.i – 1.c.viii
2. If end of record, end line; repeat 1.a. – 1.c. until end of file
NOTES:
1. The input file is binary – contents are NOT in ASCII format
2. The starting byte position for record 2 and beyond will be dependent upon the length of the previous records.
3. Record headers are fixed as 8-bytes, however, content length will vary.
4. First 100 bytes of file are irrelevant data