Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

convert single to 4 bytes 4

Status
Not open for further replies.

mcnorth

Technical User
Aug 27, 2002
17
0
0
US
Is there a way to convert a single to its 4 representative bytes?
 
Yes.
___
[tt]
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Private Sub Form_Load()
Dim S As Single
Dim B(3) As Byte
S = 123.456 'or any other value
CopyMemory B(0), S, 4
Debug.Print B(0); B(1); B(2); B(3)
End Sub[/tt]
 
Oh, that's perfect. Exactly what I needed.

Thanks for taking the time to share it!!!
 
It's also possible to do this without using CopyMemory...

Option Explicit

Private Type vbSingle
value As Single ' 4 bytes
End Type

Private Type vbDouble
value As Double ' 8 bytes
End Type

Private Type vbDeconstruct
B(7) As Byte ' needs to be at least as long as largest numeric type you want to deconstruct
End Type



Private Sub Command1_Click()
Dim mySingle As vbSingle
Dim myDouble As vbDouble
Dim des As vbDeconstruct

' Deconstruct a single
mySingle.value = 123.456
LSet des = mySingle
Debug.Print des.B(0); des.B(1); des.B(2); des.B(3)

' Now desconstruct a Double
myDouble.value = 123.456
LSet des = myDouble
Debug.Print des.B(0); des.B(1); des.B(2); des.B(3); des.B(4); des.B(5); des.B(6); des.B(7)

End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top