Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Converting single precision float to a 4 byte string

Posted on 2003-03-01
2
Medium Priority
?
854 Views
Last Modified: 2012-06-27
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)
0
Comment
Question by:dummstah
2 Comments
 
LVL 3

Accepted Solution

by:
DocM earned 200 total points
ID: 8047597

 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
 
LVL 1

Author Comment

by:dummstah
ID: 8047631
Works perfectly. Thanks a lot.
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Suggested Courses

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question