Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. VB Endian Conversion

VB Endian Conversion

Scheduled Pinned Locked Moved Visual Basic
csharpc++tutorialquestion
5 Posts 3 Posters 8 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mikasa
    wrote on last edited by
    #1

    Ok, I need to know how to Convert a Number to a BIG/LITTLE ENDIAN Number and also how to do "Left / Right Shifts" (i.e. in C++ / C# "5 << 16") in VB.NET or VB6. Anyone know how?

    O J 2 Replies Last reply
    0
    • M mikasa

      Ok, I need to know how to Convert a Number to a BIG/LITTLE ENDIAN Number and also how to do "Left / Right Shifts" (i.e. in C++ / C# "5 << 16") in VB.NET or VB6. Anyone know how?

      O Offline
      O Offline
      Oscar Martin
      wrote on last edited by
      #2

      Hi, Read this aticle of new version of VB.NET 2003, "Shifting to the Left or the Right" point: http://msdn.microsoft.com/library/en-us/dv\_vstechart/html/vbconVisualBasicNET2003LanguageChanges.asp?frame=true#vbconvisualbasicnet2003languagechangesanchor4 "On the 8th day, God started debugging"

      1 Reply Last reply
      0
      • M mikasa

        Ok, I need to know how to Convert a Number to a BIG/LITTLE ENDIAN Number and also how to do "Left / Right Shifts" (i.e. in C++ / C# "5 << 16") in VB.NET or VB6. Anyone know how?

        J Offline
        J Offline
        J Dunlap
        wrote on last edited by
        #3

        Here's your VB6 code:

        'MakeDWord
        'MakeWord
        'HiByte
        'LoByte
        'HiWord
        'LoWord
        'RShWord
        'LShWord
        'LShDWord
        'RShDWord
        'DWordWSwap
        'WordBSwap
        'ToggleBit
        'SetBit
        'ClearBit
        'SetBitVal
        'IsBitSet

        Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)

        'BBBB Y Y TTTTT EEEEE
        'B B Y Y T E
        'BBBB Y T EEEEE
        'B B Y T E
        'BBBB Y T EEEEE

        '***************************

        'Extract
        '*********

        Function LoWord(ByVal dw As Long) As Integer
        CopyMemory LoWord, dw, 2
        End Function

        Function HiWord(ByVal dw As Long) As Integer
        CopyMemory HiWord, ByVal VarPtr(dw) + 2, 2
        End Function

        Function LoByte(ByVal dw As Integer) As Integer
        CopyMemory LoByte, dw, 1
        End Function

        Function HiByte(ByVal dw As Integer) As Integer
        CopyMemory HiByte, ByVal VarPtr(dw) + 1, 1
        End Function

        'Swap
        '*****
        '(Little <--> Big Endian)

        Function WordBSwap(ByVal Wd As Integer) As Integer
        WordBSwap = MakeWord(HiByte(Wd), LoByte(Wd))
        End Function

        Function DWordWSwap(ByVal dw As Integer) As Integer
        DWordWSwap = MakeDWord(HiWord(dw), LoWord(dw))
        End Function

        'Bit Shift
        '**********

        Function LShWord(ByVal Wd As Integer) As Integer
        LShWord = MakeWord(HiByte(Wd), 0)
        End Function

        Function RShWord(ByVal Wd As Integer) As Integer
        RShWord = MakeWord(0, LoByte(Wd))
        End Function

        Function LShDWord(ByVal dw As Long) As Long
        LShDWord = MakeDWord(HiWord(dw), 0)
        End Function

        Function RShDWord(ByVal dw As Long) As Long
        RShDWord = MakeDWord(0, LoWord(dw))
        End Function

        'Create
        '***********

        Function MakeWord(iLo As Integer, iHi As Integer) As Integer
        CopyMemory MakeWord, iLo, 1
        CopyMemory ByVal VarPtr(MakeWord) + 1, iHi, 1
        End Function

        Function MakeDWord(iLo As Integer, iHi As Integer) As Long
        CopyMemory MakeDWord, iLo, 2
        CopyMemory ByVal VarPtr(MakeWord) + 2, iHi, 2
        End Function

        'BBBB IIIII TTTTT
        'B B I T
        'BBBB I T
        'B B I T
        'BBBB IIIII T

        '***************************

        Sub SetBit(refByte As Integer, Bit As Integer)
        ' Create a bitmask with the 2 to the nth power bit set:
        Mask = 2 ^ Bit
        ' Set the nth Bit:
        refByte = refByte Or Mask
        End Sub

        Sub ToggleBit(refByte As Integer, Bit As Integer)
        ' Create a bitmask with the 2 to the nth power bit set:
        Mask% = 2 ^ Bit
        ' Toggle the nth Bit:
        refByte = refByte Xor Mask%
        End Sub

        ' The ClearBit Sub clears the nth bit (Bit

        M 1 Reply Last reply
        0
        • J J Dunlap

          Here's your VB6 code:

          'MakeDWord
          'MakeWord
          'HiByte
          'LoByte
          'HiWord
          'LoWord
          'RShWord
          'LShWord
          'LShDWord
          'RShDWord
          'DWordWSwap
          'WordBSwap
          'ToggleBit
          'SetBit
          'ClearBit
          'SetBitVal
          'IsBitSet

          Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)

          'BBBB Y Y TTTTT EEEEE
          'B B Y Y T E
          'BBBB Y T EEEEE
          'B B Y T E
          'BBBB Y T EEEEE

          '***************************

          'Extract
          '*********

          Function LoWord(ByVal dw As Long) As Integer
          CopyMemory LoWord, dw, 2
          End Function

          Function HiWord(ByVal dw As Long) As Integer
          CopyMemory HiWord, ByVal VarPtr(dw) + 2, 2
          End Function

          Function LoByte(ByVal dw As Integer) As Integer
          CopyMemory LoByte, dw, 1
          End Function

          Function HiByte(ByVal dw As Integer) As Integer
          CopyMemory HiByte, ByVal VarPtr(dw) + 1, 1
          End Function

          'Swap
          '*****
          '(Little <--> Big Endian)

          Function WordBSwap(ByVal Wd As Integer) As Integer
          WordBSwap = MakeWord(HiByte(Wd), LoByte(Wd))
          End Function

          Function DWordWSwap(ByVal dw As Integer) As Integer
          DWordWSwap = MakeDWord(HiWord(dw), LoWord(dw))
          End Function

          'Bit Shift
          '**********

          Function LShWord(ByVal Wd As Integer) As Integer
          LShWord = MakeWord(HiByte(Wd), 0)
          End Function

          Function RShWord(ByVal Wd As Integer) As Integer
          RShWord = MakeWord(0, LoByte(Wd))
          End Function

          Function LShDWord(ByVal dw As Long) As Long
          LShDWord = MakeDWord(HiWord(dw), 0)
          End Function

          Function RShDWord(ByVal dw As Long) As Long
          RShDWord = MakeDWord(0, LoWord(dw))
          End Function

          'Create
          '***********

          Function MakeWord(iLo As Integer, iHi As Integer) As Integer
          CopyMemory MakeWord, iLo, 1
          CopyMemory ByVal VarPtr(MakeWord) + 1, iHi, 1
          End Function

          Function MakeDWord(iLo As Integer, iHi As Integer) As Long
          CopyMemory MakeDWord, iLo, 2
          CopyMemory ByVal VarPtr(MakeWord) + 2, iHi, 2
          End Function

          'BBBB IIIII TTTTT
          'B B I T
          'BBBB I T
          'B B I T
          'BBBB IIIII T

          '***************************

          Sub SetBit(refByte As Integer, Bit As Integer)
          ' Create a bitmask with the 2 to the nth power bit set:
          Mask = 2 ^ Bit
          ' Set the nth Bit:
          refByte = refByte Or Mask
          End Sub

          Sub ToggleBit(refByte As Integer, Bit As Integer)
          ' Create a bitmask with the 2 to the nth power bit set:
          Mask% = 2 ^ Bit
          ' Toggle the nth Bit:
          refByte = refByte Xor Mask%
          End Sub

          ' The ClearBit Sub clears the nth bit (Bit

          M Offline
          M Offline
          mikasa
          wrote on last edited by
          #4

          Thanks! Good functions! Basically I wanted to know how to "Left / Right Shift" a number but the above functions will also come in handy. I will Post here how to do Shifts. To Right Shift, simply Divide by (2 ^ [No of Bits]). For example, to Right Shift 16Bits:

          Public Function ShiftRight(ByVal Value as Long) As Long
          Return (Value / (2 ^ 16))
          End Function

          To Left Shift, simply do the opposite and Multiply by (2 ^ [No of Bits]).

          Public Function ShiftLeft(ByVal Value as Long) As Long
          Return (Value * (2 ^ 16))
          End Function

          The good news is that in VB.NET 2003, the Left / Right Shift operators will become standard. So now the above Functions would be simplified like this:

          Public Function ShiftLeft(ByVal Value as Long) As Long
          Return (Value << 16)
          End Function

          Of course, you really wouldn't need a Function for it anymore in .NET 2003! ;P

          J 1 Reply Last reply
          0
          • M mikasa

            Thanks! Good functions! Basically I wanted to know how to "Left / Right Shift" a number but the above functions will also come in handy. I will Post here how to do Shifts. To Right Shift, simply Divide by (2 ^ [No of Bits]). For example, to Right Shift 16Bits:

            Public Function ShiftRight(ByVal Value as Long) As Long
            Return (Value / (2 ^ 16))
            End Function

            To Left Shift, simply do the opposite and Multiply by (2 ^ [No of Bits]).

            Public Function ShiftLeft(ByVal Value as Long) As Long
            Return (Value * (2 ^ 16))
            End Function

            The good news is that in VB.NET 2003, the Left / Right Shift operators will become standard. So now the above Functions would be simplified like this:

            Public Function ShiftLeft(ByVal Value as Long) As Long
            Return (Value << 16)
            End Function

            Of course, you really wouldn't need a Function for it anymore in .NET 2003! ;P

            J Offline
            J Offline
            J Dunlap
            wrote on last edited by
            #5

            Glad you like them! That is the cByteFns class from my Data dll.
            Do unto others as you would have them do unto you - Jesus
            An eye for an eye only makes the whole world blind - Mahatma Gandhi

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups