-
Jan 5th, 2018, 04:14 AM
#1
Thread Starter
Lively Member
Endianess
Hi
Following the work I'm currently doing, I stumbled with something. Does someone has by chance the routines to turn a double or single number with an endianess format (little or big) into the reverse?
So, what I'm looking for is:
given a double or single number, transform it to bytes (8 or 4), reverse them, and retrive the result in the same variable.
So far, I got this:
Code:
Option Explicit
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" ( _
ByRef Destination As Any, _
ByRef Source As Any, _
ByVal Length As Long)
'Double to bytes
Sub DblToByte(ByVal D As Double)
Dim Bytes(LenB(D) - 1) As Byte
Dim I As Integer
Dim S As String
CopyMemory Bytes(0), D, LenB(D)
For I = 0 To UBound(Bytes)
S = S & CStr(Bytes(I)) & " "
Next
'S now is a string bytes of the double
End Sub
'Single to bytes
Sub SngToByte(ByVal D As Single)
Dim Bytes(LenB(D) - 1) As Byte
Dim I As Integer
Dim S As String
CopyMemory Bytes(0), D, LenB(D)
For I = 0 To UBound(Bytes)
S = S & CStr(Bytes(I)) & " "
Next
'S now is a string bytes of the single
End Sub
Is this right?
So I think my only problem now is convert in both cases S to a number (single or double).
Any thoughts?
Kind regards
JKepler
-
Jan 5th, 2018, 08:00 AM
#2
Re: Endianess
WARNING: Air code ahead...
Code:
Public Function DblToBytes(ByVal Value As Double) As Byte()
Dim RetBytes(0 To 7) As Byte
CopyMemory RetBytes(0), Value, LenB(Value)
DblToBytes = RetBytes
End Function
Public Function ReverseBytes(Bytes() As Byte) As Byte()
ReDim RetBytes(0 To UBound(Bytes)) As Byte
Dim Index As Long
For Index = 0 To UBound(Bytes)
RetBytes(Index) = Bytes(UBound(Bytes) - Index)
Next
ReverseBytes = RetBytes
End Function
Public Function BytesToDbl(Bytes() As Byte) As Double
CopyMemory BytesToDbl, Bytes(0), LenB(BytesToDbl)
End Function
edit: doesn't work! not sure wher e it's broke. time to bust out the IDE.
ok it does work... nothing to see here.
Last edited by DEXWERX; Jan 5th, 2018 at 08:07 AM.
-
Jan 5th, 2018, 08:07 AM
#3
Thread Starter
Lively Member
Re: Endianess
ohhh...this is good... very good indeed......
-
Jan 10th, 2018, 07:49 PM
#4
Member
Re: Endianess
Here is a class for bit manipulation that also has a ReverseBytes routine. The procedure will handle any data type. Or you can extract the code.
rdee
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|