Working with Bits and Bytes in .Net
While .Net is great for developing large, enterprise-level applications I have found it to be lacking basic manipulation abilities. When developing applications that involve low-level microcontrollers I often have struggled to find the correct syntax to do proper bit manipulation. Furthermore debugging byte arrays can get plain frustrating. As you are probably aware, the IDE displays bytes as numeric values (0-255) instead of the standard hexadecimal notation. This is fine and dandy but the majority (>99%) of technical documentation has chosen hexadecimal notation for representation of byte streams in protocols and interfaces. Translating can get tedious for those who do not speak fluent hex, a group of individuals that I currently belong to.
The .Net library is great but I feel it is lacking some easy-to-use tools for bit-manipulation. Using Visual Basic of course doesn’t help things. To solve these problems I have created a set of basic bit-manipulating and debugging tools that I include in my base classes in order to output hexadecimal strings of byte arrays and to flip bits as needed. I have found these utilities invaluable when building low-level interface code that talks via USB or serial to devices that don’t support the latest XML object serialization techniques for remoting cutting-edge .Net entity framework model do-dads and devices that don’t have the luxury to run the full CLI.
Polling the Status of a Bit
Protected Function GetBit(ByVal Byte As Byte, ByVal BitPosition As Integer) As Boolean
Return ((Byte And 2 ^ BitPosition) <> False)
End Function
Setting a bit
Protected Sub SetBit(ByRef oByte As Byte, ByVal BitPosition As Integer, ByVal NewPosition As Boolean)
Dim newByte As Byte = oByte
If ((oByte And 2 ^ BitPosition) <> False And NewPosition = False) Then
newByte = oByte Xor 2 ^ (BitPosition)
ElseIf ((oByte And 2 ^ BitPosition) = False And NewPosition = True) Then
newByte = oByte Or 2 ^ (BitPosition)
End If
oByte = newByte
End Sub
Converting to ASCII Hexadecimal Representation from Bytes
Public Function ByteToHex(ByVal comByte As Byte()) As String
'create a new StringBuilder object
Dim builder As New System.Text.StringBuilder(comByte.Length * 3)
'loop through each byte in the array
For Each data As Byte In comByte
builder.Append(Convert.ToString(data, 16).PadLeft(2, "0"c).PadRight(3, " "c))
'convert the byte to a string and add to the stringbuilder
Next
'return the converted value
Return builder.ToString().ToUpper()
End Function
Converting from ASCII Hexadecimal Representation to Bytes
Public Function HexToByte(ByVal msg As String) As Byte()
If msg.Length Mod 2 = 0 Then
'remove any spaces from the string
Dim _msg As String
_msg = msg
_msg = msg.Replace(" ", "")
'create a byte array the length of the
'divided by 2 (Hex is 2 characters in length)
'Dim comBuffer As Byte() = New Byte(_msg.Length / 2 - 1) {}
Dim comBuffer((_msg.Length / 2) - 1) As Byte
For i As Integer = 0 To _msg.Length - 1 Step 2
comBuffer(i / 2) = CByte(Convert.ToByte(_msg.Substring(i, 2), 16))
Next
'loop through the length of the provided string
'convert each set of 2 characters to a byte
'and add to the array
'return the array
Return comBuffer
Else
Dim ABuffer() As Byte = {0, 0}
Return ABuffer
End If
End Function
Hopefully you will find these functions as useful as I have over the years!