I have come up with my own generic Base32 encoder/decoder implementation for VB.NET. I have verified the results through independent web sites so it does appear to be quite accurate.
Any comments on how improvements could be made to the code will be welcomed.
Option Compare Text
Imports System.ComponentModel
Public Structure Base32(Of T)
Private Const csValidStandardBase32Chars As String = "0123456789ABCDEFGHIJKLMNOPQRSTUV"
<EditorBrowsable(EditorBrowsableState.Never)> _
Class Base32Nibble
<EditorBrowsable(EditorBrowsableState.Never)> _
Friend mStore As New BitArray(5, False)
Public ReadOnly Property Value As Byte
Get
Dim result As Byte = 0
For index As Byte = 0 To mStore.Count - 1
If mStore(index) Then
result += (2 ^ index)
End If
Next
Return result
End Get
End Property
Public Overrides Function ToString() As String
Dim nibbleString As String = Nothing
For Each bit As Boolean In mStore
nibbleString = Math.Abs(CInt(bit)).ToString() & nibbleString
Next
Return nibbleString
End Function
End Class
Private Shared mNibbles As List(Of Base32Nibble)
Public ReadOnly Property Count As Long
Get
Return mNibbles.Count
End Get
End Property
Default Public ReadOnly Property Item(ByVal index As Integer) As Base32Nibble
Get
Return DirectCast(mNibbles(index), Base32Nibble)
End Get
End Property
Public Sub New(ByVal Value As T)
Dim temp As Object = CType(Value, T)
getNibbles(BitConverter.GetBytes(temp))
End Sub
Public Sub New(ByVal Value As Byte())
getNibbles(Value)
End Sub
Public Shared Widening Operator CType(ByVal Value As T) As Base32(Of T)
Return New Base32(Of T)(Value)
End Operator
Public Shared Widening Operator CType(ByVal Value As Byte()) As Base32(Of T)
Return New Base32(Of T)(Value)
End Operator
Public ReadOnly Property Value As String
Get
Dim result As String = Nothing
For Each Nib As Base32(Of T).Base32Nibble In mNibbles
result = csValidStandardBase32Chars(Nib.Value) & result
Next
Return result.TrimStart("0")
End Get
End Property
Public Function ToNumeric(ByVal Base32String As String) As T
Dim result As T = CType(CType(0, Object), T)
Try
If Base32String.Trim.Length > 0 Then
Dim pos As Integer = 0
Do
Dim temp As Object = getBase32Value(Base32String, pos)
result = result + temp
pos += 1
Loop While (pos < Base32String.Length)
End If
Catch ex As Exception
' Catch overflow errors if the generic type T doesn't have enough
' room to store the result
System.Diagnostics.Debug.Print(ex.Message)
End Try
Return result
End Function
Private Shared Sub getNibbles(ByVal Value As Byte())
Dim valueBytes As New BitArray(Value)
Dim nib As Base32Nibble = Nothing
mNibbles = New List(Of Base32Nibble)
Dim padding As Byte = (1 - (valueBytes.Length / 5 - (valueBytes.Length \ 5))) * 5
valueBytes.Length = valueBytes.Length + padding
For element As Short = 0 To valueBytes.Count - 1
If (element Mod 5 = 0) Then
nib = New Base32Nibble()
mNibbles.Add(nib)
End If
nib.mStore.Item(element Mod 5) = valueBytes.Item(element)
Next
End Sub
Private Function getBase32Char(ByVal InputString As String, ByVal InputPosition As Integer) As String
Return csValidStandardBase32Chars.IndexOf(Mid(InputString, InputPosition + 1, 1))
End Function
Private Function getBase32Value(ByVal InputString As String, ByVal InputPosition As Integer) As T
Return CType(CType((getBase32Char(InputString, InputPosition) * (32 ^ (InputString.Length - 1 - InputPosition))), Object), T)
End Function
End Structure
Here is an example of converting 123456789 to Base32
Dim value As Base32(Of Int64) = 123456789
Console.WriteLine( "123456789 in Base32 = " & value.Value)