CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Guest

    Byte Swapping in VB

    I am reading in a file which is in binary form, and has been ouputted from a machine which uses big endian format. I need to get the information into little endian format.

    I am reading in a long, and know how to get it either as a big endian long, or as a set of 4 bytes, but dont know how to go from this point, to forming the little endian long.

    Please help me!


  2. #2
    Join Date
    May 1999
    Location
    Omika, Japan
    Posts
    729

    Re: Byte Swapping in VB

    Take a look at this: If you understand what is happening, you would be able to apply for your case:

    A small little hack using copying memory blocks.


    option Explicit

    private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDest as Any, pSource as Any, byval dwLength as Long)

    private Sub Form_Load()
    Dim i as Integer
    Dim long1 as Long, long2 as Long
    Dim cbuf1(3) as Byte, cbuf2(3) as Byte
    Dim plong1 as Long, plong2 as Long

    long1 = &HBBBBFFFF
    plong1 = VarPtr(long1)
    plong2 = VarPtr(long2)
    CopyMemory byval plong2, byval (plong1 + 2), 2
    CopyMemory byval (plong2 + 2), byval plong1, 2
    Debug.print Hex$(long1) ' "BBBBFFFF"
    Debug.print Hex$(long2) ' "FFFFBBBB"

    ' USING Byte arrays: as read from a file, say:
    cbuf1(0) = &HAA
    cbuf1(1) = &HAA
    cbuf1(2) = &HFF
    cbuf1(3) = &HFF
    for i = 0 to 3
    Debug.print Hex$(cbuf1(i)); '-> "AAAAFFFF"
    next i
    Debug.print

    CopyMemory cbuf2(0), cbuf1(2), 2
    CopyMemory cbuf2(2), cbuf1(0), 2
    for i = 0 to 3
    Debug.print Hex$(cbuf2(i)); '-> "FFFFAAAA"
    next i

    End Sub




    ps: If you are using these for reading big files, check which is the best method from perf. point of view.

    RK

  3. #3
    Guest

    Re: Byte Swapping in VB

    Thank you, this was very helpful!


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

Featured