Endianess-VBForums
Results 1 to 4 of 4

Thread: Endianess

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2011
    Posts
    108

    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

  2. #2
    Frenzied Member
    Join Date
    Jun 2015
    Posts
    1,862

    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 07:07 AM.
    Imagine what it would be like to set breakpoints in, or step through subclassing code;
    and then being able to hit stop/end/debug or continue, without crashing the IDE.

    VB6.tlb | Bulletproof Subclassing in the IDE (no thunks/assembly/DEP issues)

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Mar 2011
    Posts
    108

    Re: Endianess

    ohhh...this is good... very good indeed......

  4. #4
    Member
    Join Date
    Nov 2016
    Location
    Coastal Georgia
    Posts
    40

    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
  •  



Featured


Click Here to Expand Forum to Full Width