Link to home
Start Free TrialLog in
Avatar of dummstah
dummstah

asked on

Converting single precision float to a 4 byte string

I need a function to convert a single precision floating point into a 4 byte string (so that I can story it into a binary file).

I have tried to do this by creating the function below



Public Declare Sub RtlMoveMemory Lib "kernel32" (ByVal hpvDest As Long, ByVal hpvSource As Long, ByVal cbCopy As Long)

Function floattostring(byval tFloat as single) as String
  dim sBuffer As String

  sBuffer = String(4, vbNullChar)
  RtlMoveMemory StrPtr(sBuffer), VarPtr(tFloat), 4
  floattostring = sBuffer
end function



however it doesn't seem to work (the string returned is always the 4 bytes "3F 3F 00 00").

Does anyone know a simple way I could convert this (either using memory copy/read api or actually converting it using an vb function)
ASKER CERTIFIED SOLUTION
Avatar of DocM
DocM

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dummstah
dummstah

ASKER

Works perfectly. Thanks a lot.