BitStringConverter Class
Life With Nate
Nate's Poetry Page
Resume (PDF)
Nate's Code
ASP.NET 2.0/3.5
VB.NET 2.0/3.5
BitStringConverter
Fraction
LargeDecimal
LDFraction
Transparency
Just For Fun
Windows Phone 7
Description
The BitStringConverter class contains shared functions that allow you to convert some of the value types to or from a String of 0s and 1s representing the bits.
Example
GetBits(value As SByte)
GetBits(value As Byte)
GetBits(value As Short)
GetBits(value As UShort)
GetBits(value As Integer)
GetBits(value As UInteger)
GetBits(value As Long)
GetBits(value As ULong)
GetBits(value As Double)
GetBits(value As Single)
GetBits(value As Char)
GetBits(value As Boolean)
BitsToSByte(bits As String)
BitsToByte(bits As String)
BitsToShort(bits As String)
BitsToUShort(bits As String)
BitsToInteger(bits As String)
BitsToUInteger(bits As String)
BitsToLong(bits As String)
BitsToULong(bits As String)
BitsToDouble(bits As String)
BitsToSingle(bits As String)
BitsToChar(bits As String)
BitsToBoolean(bits As String)
Properties & Methods
Converting to a Bit String
GetBits(value As SByte)
GetBits(value As Byte)
GetBits(value As Short)
GetBits(value As UShort)
GetBits(value As Integer)
GetBits(value As UInteger)
GetBits(value As Long)
GetBits(value As ULong)
GetBits(value As Double)
GetBits(value As Single)
GetBits(value As Char)
GetBits(value As Boolean)
Converting from a Bit String
BitsToSByte(bits As String)
BitsToByte(bits As String)
BitsToShort(bits As String)
BitsToUShort(bits As String)
BitsToInteger(bits As String)
BitsToUInteger(bits As String)
BitsToLong(bits As String)
BitsToULong(bits As String)
BitsToDouble(bits As String)
BitsToSingle(bits As String)
BitsToChar(bits As String)
BitsToBoolean(bits As String)
Source Code
BitStringConverter.vb:
Namespace NathanSokalski Public Class BitStringConverter Public Shared Function GetBits(value As SByte) As String Dim bits As New System.Text.StringBuilder() For i As Integer = 0 To 7 bits.Append(If(((value << i) And SByte.MinValue) = SByte.MinValue, "1", "0").ToString()) Next Return bits.ToString() End Function Public Shared Function GetBits(value As Byte) As String Return GetBitsFromArray(New Byte() {value}) End Function Public Shared Function GetBits(value As Short) As String Dim bytes() As Byte = BitConverter.GetBytes(value) Array.Reverse(bytes) Return GetBitsFromArray(bytes) End Function Public Shared Function GetBits(value As UShort) As String Dim bytes() As Byte = BitConverter.GetBytes(value) Array.Reverse(bytes) Return GetBitsFromArray(bytes) End Function Public Shared Function GetBits(value As Integer) As String Dim bytes() As Byte = BitConverter.GetBytes(value) Array.Reverse(bytes) Return GetBitsFromArray(bytes) End Function Public Shared Function GetBits(value As UInteger) As String Dim bytes() As Byte = BitConverter.GetBytes(value) Array.Reverse(bytes) Return GetBitsFromArray(bytes) End Function Public Shared Function GetBits(value As Long) As String Dim bytes() As Byte = BitConverter.GetBytes(value) Array.Reverse(bytes) Return GetBitsFromArray(bytes) End Function Public Shared Function GetBits(value As ULong) As String Dim bytes() As Byte = BitConverter.GetBytes(value) Array.Reverse(bytes) Return GetBitsFromArray(bytes) End Function Public Shared Function GetBits(value As Double) As String Dim bytes() As Byte = BitConverter.GetBytes(value) Array.Reverse(bytes) Return GetBitsFromArray(bytes) End Function Public Shared Function GetBits(value As Single) As String Dim bytes() As Byte = BitConverter.GetBytes(value) Array.Reverse(bytes) Return GetBitsFromArray(bytes) End Function Public Shared Function GetBits(value As Char) As String Dim bytes() As Byte = BitConverter.GetBytes(value) Array.Reverse(bytes) Return GetBitsFromArray(bytes) End Function Public Shared Function GetBits(value As Boolean) As String Return GetBitsFromArray(BitConverter.GetBytes(value)) End Function Private Shared Function GetBitsFromArray(bytes() As Byte) As String Dim bits As New System.Text.StringBuilder() For Each currbyte As Byte In bytes For i As Integer = 0 To 7 bits.Append(If(((currbyte << i) And CByte(128)) = CByte(128), "1", "0").ToString()) Next Next Return bits.ToString() End Function Public Shared Function BitsToSByte(bits As String) As SByte If bits.Length = 8 Then If BitsToBytes(bits)(0) > 127 Then Return CSByte(BitsToBytes(bits)(0) - 256) Else Return CSByte(BitsToBytes(bits)(0)) Else Throw New Exception("You have entered an invalid bit string. The bit string must be 8 characters and contain only 0s and 1s.") End If End Function Public Shared Function BitsToByte(bits As String) As Byte If bits.Length = 8 Then Return BitsToBytes(bits)(0) Else Throw New Exception("You have entered an invalid bit string. The bit string must be 8 characters and contain only 0s and 1s.") End Function Public Shared Function BitsToShort(bits As String) As Short If bits.Length = 16 Then Return BitConverter.ToInt16(BitsToBytes(bits), 0) Else Throw New Exception("You have entered an invalid bit string. The bit string must be 16 characters and contain only 0s and 1s.") End If End Function Public Shared Function BitsToUShort(bits As String) As UShort If bits.Length = 16 Then Return BitConverter.ToUInt16(BitsToBytes(bits), 0) Else Throw New Exception("You have entered an invalid bit string. The bit string must be 16 characters and contain only 0s and 1s.") End If End Function Public Shared Function BitsToInteger(bits As String) As Integer If bits.Length = 32 Then Return BitConverter.ToInt32(BitsToBytes(bits), 0) Else Throw New Exception("You have entered an invalid bit string. The bit string must be 32 characters and contain only 0s and 1s.") End If End Function Public Shared Function BitsToUInteger(bits As String) As UInteger If bits.Length = 32 Then Return BitConverter.ToUInt32(BitsToBytes(bits), 0) Else Throw New Exception("You have entered an invalid bit string. The bit string must be 32 characters and contain only 0s and 1s.") End If End Function Public Shared Function BitsToLong(bits As String) As Long If bits.Length = 64 Then Return BitConverter.ToInt64(BitsToBytes(bits), 0) Else Throw New Exception("You have entered an invalid bit string. The bit string must be 64 characters and contain only 0s and 1s.") End If End Function Public Shared Function BitsToULong(bits As String) As ULong If bits.Length = 64 Then Return BitConverter.ToUInt64(BitsToBytes(bits), 0) Else Throw New Exception("You have entered an invalid bit string. The bit string must be 64 characters and contain only 0s and 1s.") End If End Function Public Shared Function BitsToDouble(bits As String) As Double If bits.Length = 64 Then Return BitConverter.ToDouble(BitsToBytes(bits), 0) Else Throw New Exception("You have entered an invalid bit string. The bit string must be 64 characters and contain only 0s and 1s.") End If End Function Public Shared Function BitsToSingle(bits As String) As Single If bits.Length = 32 Then Return BitConverter.ToSingle(BitsToBytes(bits), 0) Else Throw New Exception("You have entered an invalid bit string. The bit string must be 32 characters and contain only 0s and 1s.") End If End Function Public Shared Function BitsToChar(bits As String) As Char If bits.Length = 16 Then Return BitConverter.ToChar(BitsToBytes(bits), 0) Else Throw New Exception("You have entered an invalid bit string. The bit string must be 16 characters and contain only 0s and 1s.") End If End Function Public Shared Function BitsToBoolean(bits As String) As Boolean If bits.Length = 8 Then Return BitConverter.ToBoolean(BitsToBytes(bits), 0) Else Throw New Exception("You have entered an invalid bit string. The bit string must be 8 characters and contain only 0s and 1s.") End If End Function Private Shared Function BitsToBytes(bits As String) As Byte() If bits.Length >= 8 AndAlso bits.Length Mod 8 = 0 AndAlso System.Text.RegularExpressions.Regex.IsMatch(bits, "^[01]{8,}$") Then Dim bytes(bits.Length \ 8 - 1) As Byte Dim currbyte As Byte For i As Integer = 0 To bits.Length \ 8 - 1 currbyte = 0 For j As Integer = 0 To 7 currbyte <<= 1 If bits.Chars(i * 8 + j) = "1"c Then currbyte += CByte(1) Next bytes(i) = currbyte Next Array.Reverse(bytes) Return bytes Else Throw New Exception("You have entered an invalid bit string. The bit string must be a non-zero length divisible by 8 and contain only 0s and 1s.") End If End Function End Class End Namespace
Remarks
Although not the most useful or complex class in the world, BitStringConverter may sometimes be useful when writing code that involves hardware communication or an encryption algorithm (I don't normally write either of those types of code, so I could be wrong), but either way, it never hurts to have a few more utilities available.