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)
LVL 1
dummstahAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

DocMCommented:

 Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" ( _
  hpvDest As Any, hpvSource As Any, _
  ByVal cbCopy As Long)

Private Sub Single_To_4BytesString()

Dim str1 As String
Dim sng As Single

sng = 1.23456789
 

str1 = String(4, vbNullChar)
Call CopyMemory(ByVal str1, sng, 4)
 
Debug.Print
For i = 1 To 4
 Debug.Print Asc(Mid(str1, i, 1)); " ";
Next
Debug.Print

End Sub
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dummstahAuthor Commented:
Works perfectly. Thanks a lot.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

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.