VB Endian Conversion
-
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?
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"
-
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?
Here's your VB6 code:
'MakeDWord
'MakeWord
'HiByte
'LoByte
'HiWord
'LoWord
'RShWord
'LShWord
'LShDWord
'RShDWord
'DWordWSwap
'WordBSwap
'ToggleBit
'SetBit
'ClearBit
'SetBitVal
'IsBitSetPrivate 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 FunctionFunction HiWord(ByVal dw As Long) As Integer
CopyMemory HiWord, ByVal VarPtr(dw) + 2, 2
End FunctionFunction LoByte(ByVal dw As Integer) As Integer
CopyMemory LoByte, dw, 1
End FunctionFunction 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 FunctionFunction 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 FunctionFunction RShWord(ByVal Wd As Integer) As Integer
RShWord = MakeWord(0, LoByte(Wd))
End FunctionFunction LShDWord(ByVal dw As Long) As Long
LShDWord = MakeDWord(HiWord(dw), 0)
End FunctionFunction 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 FunctionFunction 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 SubSub 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
-
Here's your VB6 code:
'MakeDWord
'MakeWord
'HiByte
'LoByte
'HiWord
'LoWord
'RShWord
'LShWord
'LShDWord
'RShDWord
'DWordWSwap
'WordBSwap
'ToggleBit
'SetBit
'ClearBit
'SetBitVal
'IsBitSetPrivate 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 FunctionFunction HiWord(ByVal dw As Long) As Integer
CopyMemory HiWord, ByVal VarPtr(dw) + 2, 2
End FunctionFunction LoByte(ByVal dw As Integer) As Integer
CopyMemory LoByte, dw, 1
End FunctionFunction 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 FunctionFunction 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 FunctionFunction RShWord(ByVal Wd As Integer) As Integer
RShWord = MakeWord(0, LoByte(Wd))
End FunctionFunction LShDWord(ByVal dw As Long) As Long
LShDWord = MakeDWord(HiWord(dw), 0)
End FunctionFunction 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 FunctionFunction 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 SubSub 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
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 FunctionTo 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 FunctionThe 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 FunctionOf course, you really wouldn't need a Function for it anymore in .NET 2003! ;P
-
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 FunctionTo 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 FunctionThe 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 FunctionOf course, you really wouldn't need a Function for it anymore in .NET 2003! ;P