Author Message

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

I'm not sure about the floating point, but the two byte integer can be be
calculated like this:
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):

local S, i, Result

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

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:
:       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):
:
:       local S, i, Result
:
:
:       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

Michael,

Thanks.
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

 Page 1 of 1 [ 4 post ]

Relevant Pages