Reading binary data files 
Author Message
 Reading binary data files

I am having trouble converting binary numeric data into the correct type
for storage in a .DBF file.  The creator of the binary file stores
numeric data of 3 types, integer (2 bytes), long integer (4 bytes), and
single precision floating point (4 bytes).  I can get the information
using FREAD(), which returns a character string.  How do I then convert
the character string to the correct type of numeric?  I have a file
definition from the creator, so I know in advance what the data type
should be.  Does anyone have any suggestions on how to solve this
problem?

Thanks,

Dave



Mon, 30 Aug 1999 03:00:00 GMT  
 Reading binary data files

I'm not sure about the floating point, but the two byte integer can be be
calculated like this:
        S = fRead(f, 2)
        N = Asc(S) + 256 * Asc(SubStr(S,2))

(This works for positive integers.)

Here is a generic solution for an integer of any length (where F is a file
and IntSize is the number of bytes in the integer):

Function ReadInt(F, IntSize)
        local S, i, Result

        S = FRead(F, IntSize)

        Result = 0
        For i =IntSize to 1 step -1
                Result = Result * 256 + Asc(SubStr(S,i))
        EndFor

        If Result >= 2 ^ (IntSize * 8 - 1)   && Is the number negative
                Result = Result - 2 ^ (IntSize * 8)
        EndIf

Return Result

--
Michael Schuldenfrei



Thu, 02 Sep 1999 03:00:00 GMT  
 Reading binary data files

I didn't see the entire post, but you should also be aware of the byte
ordering when dealing with binary files.  Some systems store integers as
most significant byte/least significant byte others are the opposite.  This
will also affect character strings and most defiantly floating point which
is almost entirely processor or system dependent.

Regards,

Steven E. Kalbach



: I'm not sure about the floating point, but the two byte integer can be be
: calculated like this:
:       S = fRead(f, 2)
:       N = Asc(S) + 256 * Asc(SubStr(S,2))
:
: (This works for positive integers.)
:
: Here is a generic solution for an integer of any length (where F is a
file
: and IntSize is the number of bytes in the integer):
:
: Function ReadInt(F, IntSize)
:       local S, i, Result
:
:       S = FRead(F, IntSize)
:
:       Result = 0
:       For i =IntSize to 1 step -1
:               Result = Result * 256 + Asc(SubStr(S,i))
:       EndFor
:
:       If Result >= 2 ^ (IntSize * 8 - 1)   && Is the number negative
:               Result = Result - 2 ^ (IntSize * 8)
:       EndIf
:
: Return Result
:
:
: --
: Michael Schuldenfrei

:
:



Thu, 02 Sep 1999 03:00:00 GMT  
 Reading binary data files

Michael,

Thanks.
Your function ReadInt doesn't seem to fit VFP's Integer fields as
returned by he BINTOC(function)

local i, x, y, z
z = 0
i = 1
x=bintoc(mytable.integer)
y=asc(substr(x,1,1)) - 128
do while i <=4
  z = z * 256 + y
  i=i+1
  y=asc(substr(x,i,1))
enddo
return z

The special case for negative values is eliminated.

-Anders

--

03/17/97 17:38
---------
Using: OUI PRO 1.5.0.2 from http://www.dvorak.com



Fri, 03 Sep 1999 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Reading binary data from a file

2. Reading MBF 8 byte fields from binary file

3. Reading a Binary File

4. Extract binary data from a file

5. General data type change to Memo(Binary) data type

6. How to read Binary values from registry?

7. Read data from excel file (VFP7)

8. Redux: Reading a DBC file as a data table

9. Reading the DBC file as a data table

10. Remove binary data

11. Converting to ADO with binary data

12. VFP to SQl server with binary data

 

 
Powered by phpBB® Forum Software