Formatted file

Hi.. I'm trying to read a binary file. The structure of the file is:

0    ~   1     1     ~     2
|TRF HRU Z|   |Id Res RecLen NF Res  FN Res  Flag  ID FD T  IT Res S Res AF Res|

TRF – Total Records on File
HRU – Highest Record available on the directory  
Z – Zeroes that take up space or it may have copyright info

Id – The number of the file   Ex: F1.Dat F1.Dop
Res – Just a space
RecLen – Record size
NF – Number of fields
FN – File Name
Flag – Data state
ID- Initial Date     Stored  in this format     (yymmdd )
FD- Final Date     Stored  in this format     (yymmdd )  
IT-Intraday Time
AF- Auto Run Flag

The Format is an old binary format that requires to be converted to a IEEE standard float.

The code was written in Delphi.

the conversion to IEEE standard float is (in delphi):
Function Ms_to_Ie(Var ms) : Real;
    ie : Array [1..6] of Byte Absolute r;
    m    : My_real Absolute ms;
    r    : Real;
  FillChar(r, sizeof(r), 0);
  ie[1] := m[4];
  ie[4] := m[1];
  ie[5] := m[2];
  ie[6] := m[3];
  Ms_to_ie := r;

The problem is...
can I read the file not with a delphi program? I mean, I make the program with other language? and how do I do that? I mean should I read the file byte per byte and translate it to ieee float? I really don't understand what to do with the file! thanks..
Who is Participating?
john_gabrielConnect With a Mentor Commented:
   With binary files, the data records
are usually of fixed length, so, you
don't need a structure. However, none
of the above fields you mentioned appear
to be of floating point type and so
your question does not appear to make any sense.

Here is an excerpt from MSDN on how floating point numbers are stored using
IEEE. If you have any floating point
values, you should read n bytes (one
record being n bytes), fetch the bytes
pertaining to the floating point number
- you can do this if you know what position these are at and then you can
rearrange the bytes to IEEE format before you write the new record back to your output file. I have done this many
times in the past and you need to look
out for big-endian and little-endian
storage format of the current file you
are trying to convert.

Type float
Floating-point numbers use the IEEE (Institute of Electrical and Electronics Engineers) format. Single-precision values with float type have 4 bytes, consisting of a sign bit, an 8-bit excess-127 binary exponent, and a 23-bit mantissa. The mantissa represents a number between 1.0 and 2.0. Since the high-order bit of the mantissa is always 1, it is not stored in the number. This representation gives a range of approximately 3.4E–38 to 3.4E+38 for type float.

You can declare variables as float or double, depending on the needs of your application. The principal differences between the two types are the significance they can represent, the storage they require, and their range. Table 3.3 shows the relationship between significance and storage requirements.

Table 3.3   Floating-Point Types

Type Significant digits Number of bytes
float 6 – 7 4
double 15 – 16 8

Floating-point variables are represented by a mantissa, which contains the value of the number, and an exponent, which contains the order of magnitude of the number.

Table 3.4 shows the number of bits allocated to the mantissa and the exponent for each floating-point type. The most significant bit of any float or double is always the sign bit. If it is 1, the number is considered negative; otherwise, it is considered a positive number.

Table 3.4   Lengths of Exponents and Mantissas

Type Exponent length Mantissa length
float 8 bits  23 bits
double 11 bits  52 bits

Because exponents are stored in an unsigned form, the exponent is biased by half its possible value. For type float, the bias is 127; for type double, it is 1023. You can compute the actual exponent value by subtracting the bias value from the exponent value.

The mantissa is stored as a binary fraction greater than or equal to 1 and less than 2. For types float and double, there is an implied leading 1 in the mantissa in the most-significant bit position, so the mantissas are actually 24 and 53 bits long, respectively, even though the most-significant bit is never stored in memory.

Instead of the storage method just described, the floating-point package can store binary floating-point numbers as denormalized numbers. “Denormalized numbers” are nonzero floating-point numbers with reserved exponent values in which the most-significant bit of the mantissa is 0. By using the denormalized format, the range of a floating-point number can be extended at the cost of precision. You cannot control whether a floating-point number is represented in normalized or denormalized form; the floating-point package determines the representation. The floating-point package never uses a denormalized form unless the exponent becomes less than the minimum that can be represented in a normalized form.

Table 3.5 shows the minimum and maximum values you can store in variables of each floating-point type. The values listed in this table apply only to normalized floating-point numbers; denormalized floating-point numbers have a smaller minimum value. Note that numbers retained in 80x87 registers are always represented in 80-bit normalized form; numbers can only be represented in denormalized form when stored in 32-bit or 64-bit floating-point variables (variables of type float and type long).

Table 3.5   Range of Floating-Point Types

Type Minimum value Maximum value
float 1.175494351 E – 38 3.402823466 E + 38
double 2.2250738585072014 E – 308 1.7976931348623158 E + 308

If precision is less of a concern than storage, consider using type float for floating-point variables. Conversely, if precision is the most important criterion, use type double.

Floating-point variables can be promoted to a type of greater significance (from type float to type double). Promotion often occurs when you perform arithmetic on floating-point variables. This arithmetic is always done in as high a degree of precision as the variable with the highest degree of precision. For example, consider the following type declarations:

float f_short;
double f_long;
long double f_longer;

f_short = f_short * f_long;

In the preceding example, the variable f_short is promoted to type double and multiplied by f_long; then the result is rounded to type float before being assigned to f_short.

In the following example (which uses the declarations from the preceding example), the arithmetic is done in float (32-bit) precision on the variables; the result is then promoted to type double:

f_longer = f_short * f_short;

You can read the file with any language that can read files in binary mode, so Delphi is be able to load it. You could define a structure or record that mimics the structure of your file, then load it and use the structure for accessing the elements.
verapsAuthor Commented:
Answer accepted
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.