Option Explicit On Module Conversions Private Const OFFSET_2 = 65536 Private Const MAXINT_2 = 32767 Private Const OFFSET_4 = 4294967296 Private Const MAXINT_4 = 2147483647 Function UnsignedToShort(ByVal Value As UShort) As Short If Value <= MAXINT_2 Then UnsignedToShort = Value Else UnsignedToShort = Value - OFFSET_2 End If End Function Function ShortToUnsigned(ByVal Value As Short) As UShort If Value < 0 Then ShortToUnsigned = Value + OFFSET_2 Else ShortToUnsigned = Value End If End Function Function UnsignedToInteger(ByVal Value As UInteger) As Integer If Value <= MAXINT_4 Then UnsignedToInteger = Value Else UnsignedToInteger = Value - OFFSET_4 End If End Function Function IntegerToUnsigned(ByVal Value As Integer) As UInteger If Value < 0 Then IntegerToUnsigned = Value + OFFSET_4 Else IntegerToUnsigned = Value End If End Function 'prende solo la parte positiva, se no 0 Function LongToUInt(ByVal Value As Long) As UInteger If Value < 0 Then LongToUInt = 0 Else LongToUInt = CUInt(Value) End If End Function 'Public Function WordToByte(ByVal Word As Int16, ByVal LevelByte As Byte) As Byte ' Dim szTmp As String ' szTmp = Hex(Word) ' While Len(szTmp) < 4 ' szTmp = "0" & szTmp ' End While ' If LevelByte = LOW_ORDER Then ' WordToByte = Val("&h" & Mid(szTmp, 3, 2)) ' ElseIf LevelByte = HIGH_ORDER Then ' WordToByte = Val("&h" & Mid(szTmp, 1, 2)) ' End If 'End Function Public Function WordToByte(ByVal Word As UInt16, ByVal LevelByte As Byte) As Byte Dim Value() As Byte = BitConverter.GetBytes(Word) WordToByte = Value(LevelByte) End Function Public Function ShortToByte(ByVal [Short] As Int16, ByVal LevelByte As Byte) As Byte Dim Value() As Byte = BitConverter.GetBytes([Short]) ShortToByte = Value(LevelByte) End Function Public Function ByteToWord(ByVal LowOrderByte As Byte, ByVal HighOrderByte As Byte) As UInt16 Dim Value() As Byte = {LowOrderByte, HighOrderByte} ByteToWord = BitConverter.ToUInt16(Value, 0) End Function Public Function ByteToShort(ByVal LowOrderByte As Byte, ByVal HighOrderByte As Byte) As Int16 Dim Value() As Byte = {LowOrderByte, HighOrderByte} ByteToShort = BitConverter.ToInt16(Value, 0) End Function Public Function UIntegerToSingle(ByVal [UInteger] As UInt32) As Single Dim Value() As Byte = BitConverter.GetBytes([UInteger]) UIntegerToSingle = BitConverter.ToSingle(Value, 0) End Function Public Function SingleToUInteger(ByVal [Single] As Single) As UInt32 Dim Value() As Byte = BitConverter.GetBytes([Single]) SingleToUInteger = BitConverter.ToUInt32(Value, 0) End Function Public Function DWordToWord(ByVal DWord As UInt32, ByVal LevelWord As Byte) As UInt16 Dim Value() As Byte = BitConverter.GetBytes(DWord) DWordToWord = ByteToWord(Value(0 + 2 * LevelWord), Value(1 + 2 * LevelWord)) End Function Public Function DWordToShort(ByVal DWord As UInt32, ByVal LevelWord As Byte) As Int16 Dim Value() As Byte = BitConverter.GetBytes(DWord) DWordToShort = ByteToShort(Value(0 + 2 * LevelWord), Value(1 + 2 * LevelWord)) End Function Public Function IntegerToWord(ByVal [Integer] As Int32, ByVal LevelWord As Byte) As UInt16 Dim Value() As Byte = BitConverter.GetBytes([Integer]) IntegerToWord = ByteToWord(Value(0 + 2 * LevelWord), Value(1 + 2 * LevelWord)) End Function Public Function IntegerToShort(ByVal [Integer] As Int32, ByVal LevelWord As Byte) As Int16 Dim Value() As Byte = BitConverter.GetBytes([Integer]) IntegerToShort = ByteToShort(Value(0 + 2 * LevelWord), Value(1 + 2 * LevelWord)) End Function Public Function WordsToDWord(ByVal LowOrderWord As UInt16, ByVal HighOrderWord As UInt16) As UInt32 Dim ValueLow() As Byte = BitConverter.GetBytes(LowOrderWord) Dim ValueHigh() As Byte = BitConverter.GetBytes(HighOrderWord) Dim Value() As Byte = {ValueLow(0), ValueLow(1), ValueHigh(0), ValueHigh(1)} WordsToDWord = BitConverter.ToUInt32(Value, 0) End Function Public Function WordsToInteger(ByVal LowOrderWord As UInt16, ByVal HighOrderWord As UInt16) As Int32 Dim ValueLow() As Byte = BitConverter.GetBytes(LowOrderWord) Dim ValueHigh() As Byte = BitConverter.GetBytes(HighOrderWord) Dim Value() As Byte = {ValueLow(0), ValueLow(1), ValueHigh(0), ValueHigh(1)} WordsToInteger = BitConverter.ToInt32(Value, 0) End Function Public Function ShortsToDWord(ByVal LowOrderShort As Int16, ByVal HighOrderShort As Int16) As UInt32 Dim ValueLow() As Byte = BitConverter.GetBytes(LowOrderShort) Dim ValueHigh() As Byte = BitConverter.GetBytes(HighOrderShort) Dim Value() As Byte = {ValueLow(0), ValueLow(1), ValueHigh(0), ValueHigh(1)} ShortsToDWord = BitConverter.ToUInt32(Value, 0) End Function Public Function ShortsToInteger(ByVal LowOrderShort As Int16, ByVal HighOrderShort As Int16) As Int32 Dim ValueLow() As Byte = BitConverter.GetBytes(LowOrderShort) Dim ValueHigh() As Byte = BitConverter.GetBytes(HighOrderShort) Dim Value() As Byte = {ValueLow(0), ValueLow(1), ValueHigh(0), ValueHigh(1)} ShortsToInteger = BitConverter.ToInt32(Value, 0) End Function Function ArraysEqual(ByVal first As Int16(), ByVal second As Int16()) As Boolean If (first Is second) Then Return True End If If (first Is Nothing OrElse second Is Nothing) Then Return False End If If (first.Length <> second.Length) Then Return False End If For i As Integer = 0 To first.Length - 1 If (first(i) <> second(i)) Then Return False End If Next i Return True End Function End Module