Results 1 to 6 of 6

Thread: What is the correct way for this function.

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2001
    Posts
    33

    What is the correct way for this function.

    I am converting some Delphi code to VB These 2 routines below are my attemp to make the functions work:
    Code:
    Private Function SwapBytes(theBytes() As Byte, index As Integer, count As Integer) As Variant
    Dim result() As Byte
    Dim i As Integer
    Dim ix As Integer
    ReDim result(count) As Byte
        ix = count - 1
        For i = 0 To (count - 1)
            result(ix - i) = theBytes(index + i)
        Next
        SwapBytes = result
    End Function
    
    Private Function GetQUInt32() As Double
    GetQUInt32 = (SwapBytes(Data, iStart, 4))
    iStart = iStart + 4
    
    End Function
    I guess I am using the wrong variables as SwapBytes equal nothing and when the Swapbytes function returns I get a type mismatch error .
    Any help would be appreciated..

    Rick


    This is the original Delphi code I'm converting from:
    Function TFormWSJTXUDPServer.SwapBytes(Const theBytes : TIdBytes; Const Index, Count : Integer) : TIDBytes;
    var
    i, ix : Integer;
    Begin
    SetLength(Result, Count);
    ix := Count - 1;
    For i := 0 to Count - 1 do
    begin
    Result[ix - i] := theBytes[Index + i];
    end;
    end;
    Function TFormWSJTXUDPServer.GetQUInt32(Const AData: TIdBytes) : UInt32;
    begin
    Result := PUInt32(@SwapBytes(AData, iStart, 4)[0])^;
    iStart := iStart + 4;
    end;

  2. #2
    Fanatic Member Spooman's Avatar
    Join Date
    Mar 2017
    Posts
    868

    Re: What is the correct way for this function.

    Rick

    Sorry, I don't know Delphi.
    But, perhaps you could
    1. explain what you are trying to accomplish
    2. give us some sample data
    3. give us the corresponding expected results


    Spoo

  3. #3

    Thread Starter
    Member
    Join Date
    Mar 2001
    Posts
    33

    Re: What is the correct way for this function.

    The first function takes a reference to an array of bytes, takes a segment of the array of length (Count), starting at position (Index), and reverses the order of bytes in the output array.
    The calling routine takes an array of 4 bytes reverses their order and interprets the result as a 32 byte integer.

    This is what the referencing notes say.. Its mainly to convert from a Big Endian order to a Little Endian order.

    Rick

  4. #4
    Fanatic Member Spooman's Avatar
    Join Date
    Mar 2017
    Posts
    868

    Re: What is the correct way for this function.

    Rick

    Maybe this Wiki link might help

    Spoo

  5. #5
    PowerPoster
    Join Date
    Jun 2015
    Posts
    2,224

    Re: What is the correct way for this function.

    Is iStart a module level variable? That means those functions are dependent on the Form Module... https://en.wikipedia.org/wiki/Code_smell


    GetQUint32 is using pointer magic to reinterpret cast your bytes to a uint32.

    you'll need this declare to do the equivelent
    Code:
    Private Declare Function GetMem4 Lib "msvbvm60" (Src As Any, Dst As Any) As Long
    
    Public Function SwapBytes(ByRef Bytes() As Byte, ByVal Index As Long, ByVal Count As Long) As Byte()
        ReDim Result(Count - 1) As Byte
        Dim i&, ix&: ix = Count - 1
        For i = 0 To Count - 1
            Result(ix - i) = Bytes(Index + i)
        Next
        SwapBytes = Result
    End Function
    
    
    Public Function GetQUInt32(AData() As Byte) As Double
        Dim MyLong&
        GetMem4 SwapBytes(AData, iStart, 4)(0), MyLong
        If MyLong < 0 Then
            GetQUInt32 = (MyLong And &H7FFFFFFF) + 2147483648#
        Else
            GetQUInt32 = MyLong
        End If
        iStart = iStart + 4
    End Function

    edit: if you want an actual unsigned value, I added the conversion to double you were intending.


    edit2: or if you use my typelib, it makes dealing with pointers and conversions easier.

    VB Code:
    1. Private Declare Function ntohl Lib "ws2_32" (ByVal netlong As Long) As Long
    2. Public Function GetQUInt32(AData() As Byte) As Double
    3.     'GetQUInt32 = CULngToDbl(RLng(SwapBytes(AData, iStart, 4)(0)))
    4.     GetQUInt32 = CULngToDbl(ntohl(RLng(AData(iStart))))
    5.     iStart = iStart + 4
    6. End Function

    RLng() => reinterpret as long.
    It's functionally equivalent to PUInt32(@ )^
    and because you want the unsigned value CUlngToDbl will correctly convert the sign bit.
    it's an alias to VarR8FromUI4
    Last edited by DEXWERX; Oct 20th, 2017 at 08:49 AM. Reason: added unsigned conversion to double

  6. #6

    Thread Starter
    Member
    Join Date
    Mar 2001
    Posts
    33

    Re: What is the correct way for this function.

    Thank you for both of these.. I am leaving for the weekend but I copied both of these to my laptop to try out. Will let you know how they work out..
    Thanks again..

    Rick

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width