Base32 Encoding and Decoding Sample code RRS feed

  • Question

  • Hi Team ,

     Is there any Dotnet inbuilt Base32Encode and Decode functionality available.




    Friday, March 1, 2019 3:50 AM

All replies

  • Hi meera12,

    Thank you for posting here.

    For your question, you could refer to the examples in the links below.



    Best Regards,


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, March 4, 2019 1:57 AM
  • I have not found any builtin function like that.

    You can try this one. The efficiency has not been tested but it works

      Dim ValidChars As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"
      Function bin2dec(bin As String) As Integer
            Dim res As Integer = 0
            For i As Integer = 0 To bin.Length - 1
                If bin(bin.Length - 1 - i) = "1"c Then
                    res += (2 ^ i)
                End If
            Return res
        End Function
        Public Function ToBase32String2(ByVal bytes() As Byte) As String
            Dim sb As StringBuilder = New StringBuilder
            Dim bin As String = ""
            For i As Integer = 0 To bytes.Length - 1
            	'convert each byte to 8-bit
                bin &= Convert.ToString(bytes(i), 2).PadLeft(8, "0"c)
            'group binary into 5-bit, convert to decimal and lookup in base32 character set
            Dim index As Integer = 0
            While index < bin.Length
                Dim binstr As String = bin.Substring(index)
                'make sure every segment is 5-bits
                If binstr.Length > 5 Then
                	binstr = bin.Substring(index, 5)
                	binstr = binstr.PadRight(5, "0"c)
                End If
                index += 5
            End While
            Return sb.ToString
        End Function
        Public Function FromBase32String2(ByVal str As String) As Byte()
            'Convert all charaters to uppercase
            str = str.ToUpper
            Dim bytes As New List(Of Byte)
            Dim bin As String = ""
            'Look up the position of each charater in Charater set(validChars) and Convert each position index to 5-bit
            For i As Integer = 0 To str.Length - 1
                If ValidChars.Contains(str(i)) Then
                    bin &= Convert.ToString(ValidChars.LastIndexOf(str(i)), 2).PadLeft(5, "0"c)
                    'Return nothing if invalid char is detected
                    Return Nothing
                End If
            'Group binary into 8-bit (byte) and convert
            Dim index As Integer = 0
            While index < bin.Length
                Dim binstr As String = bin.Substring(index)
                If binstr.Length > 8 Then
                	binstr = bin.Substring(index, 8)
                	binstr = binstr.PadRight(8, "0"c)
                End If
                bytes.Add(CType(bin2dec(binstr), Byte))
                index += 8
            End While
            Return bytes.ToArray
        End Function
    	Sub Button1Click(sender As Object, e As EventArgs)
    		'String to encode		
    		Dim str As String = textbox1.Text
    		'Convert to byte array		
    		Dim ba() As Byte = system.Text.Encoding.UTF8.GetBytes(str)
    		'Finally to base 32		
    		textbox2.Text = ToBase32String2(ba)
    	End Sub

    • Edited by Nforndzi Wednesday, June 26, 2019 7:54 AM
    Wednesday, June 26, 2019 7:52 AM