What I did was simply map the bytes using LSet and user-defined types. I use these as the basic conversions. If I am dealing with Motorola numbers in byte-form I simple reverse the bytes as they’re inserted into b1, b2, etc. These are also handy when you have 1, 2, or 3 bytes and need to make a long– but don’t forget to pre-set the appropriate unprovided bytes with -1 for sign extension.
Here’s what I did for converting bytes to long, single and double:
'These are used to convert bytes to integer. Individual byte values are
'put into the various positions (byte1 in b1, etc) and LSet moves the
'byte 'groups' to the number type indicated where the numeric
'equivalent is then available. Note: I gathered these from different
'places so the parameters look different. The best 'model' would be
'FourBytesToLong.
Private Type EightBytes
b1 As Byte
b2 As Byte
b3 As Byte
b4 As Byte
b5 As Byte
b6 As Byte
b7 As Byte
b8 As Byte
End Type
Private Type FourBytes
b1 As Byte
b2 As Byte
b3 As Byte
b4 As Byte
End Type
Private Type OneDouble
dD As Double
End Type
Private Type OneSingle
sngS As Single
End Type
Private Type OneLong
lL As Long
End Type
Public Function EightBytesToDouble(ByVal packetData, dataStart As Integer) As Double
'Given 64 bits of a float number (in eight bytes), returns those 64 bits
'as a double (float).
Dim EB As EightBytes
Dim OD As OneDouble
' Set the 8 input bytes into a user-defined-type 8-byte field.
EB.b1 = packetData(dataStart)
EB.b2 = packetData(dataStart + 1)
EB.b3 = packetData(dataStart + 2)
EB.b4 = packetData(dataStart + 3)
EB.b5 = packetData(dataStart + 4)
EB.b6 = packetData(dataStart + 5)
EB.b7 = packetData(dataStart + 6)
EB.b8 = packetData(dataStart + 7)
' Now move the 8 bytes to a user-defined-type double field.
' LSet and user-defined fields allow this bit-for-bit move
LSet OD = EB
'Now return the 8 bytes as a double.
EightBytesToDouble = OD.dD
End Function
Private Function FourBytesToLong(ByVal bFour) As Long
'Given 32 bits (in four bytes), returns those 32 bits as a long integer.
Dim FB As FourBytes
Dim OL As OneLong
' Set the 4 input bytes into a user-defined 4-byte field.
FB.b1 = bFour(0)
FB.b2 = bFour(1)
FB.b3 = bFour(2)
FB.b4 = bFour(3)
' Now move the 4 bytes to a user-defined long number field.
' LSet and user-defined fields allow this bit-for-bit move
LSet OL = FB
'Now return the 4 bytes as a long number.
FourBytesToLong = OL.lL
End Function
Public Function FourBytesToSingle(ByVal bFour) As Single
'Given 32 bits of a float (in four bytes), returns those 32
'bits as a single float.
Dim FB As FourBytes
Dim OS As OneSingle
' Set the 4 input bytes into a user-defined-type 4-byte field.
FB.b1 = bFour(0)
FB.b2 = bFour(1)
FB.b3 = bFour(2)
FB.b4 = bFour(3)
' Now move the 4 bytes to a user-defined-type single float field.
' LSet and user-defined-type fields allow this bit-for-bit move
LSet OS = FB
'Now return the 4 bytes as a single float number.
FourBytesToSingle = OS.sngS
End Function
Public Function TwoBytesToLong(ByVal bTwo) As Long
'Given 16 bits (in two bytes, lo-value, then hi-value), returns those
'16 bits as a long integer. Since VB does not allow unsigned integers,
'this takes the two bytes containing an unsigned integer value
'(0 - 65535) and creates a long integer that preserves the value.
Dim FB As FourBytes
Dim OL As OneLong
' Set the 2 input bytes into a user-defined 4-byte field.
FB.b1 = bTwo(0)
FB.b2 = bTwo(1)
FB.b3 = 0
FB.b4 = 0
' Now move the 4 bytes to a user-defined long number field.
' LSet and user-defined fields allow this bit-for-bit move
LSet OL = FB
'Now return the 4 bytes as a long number.
TwoBytesToLong = OL.lL
End Function
Tip Submitted by: Dave Hunt
Discover more from dotNetTips.com
Subscribe to get the latest posts sent to your email.
