none
Base32 Encoding and Decoding Sample code RRS feed

All replies

  • Hi meera12,

    Thank you for posting here.

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

    https://blogs.technet.microsoft.com/cloudpfe/2018/04/18/base32-encoding-and-decoding-in-c/

    https://gist.github.com/BravoTango86/2a085185c3b9bd8383a1f956600e515f

    Best Regards,

    Wendy


    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
    Moderator
  • 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
            Next
            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)
            Next
            
            '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)
                Else
                	binstr = binstr.PadRight(5, "0"c)
                End If
                sb.Append(ValidChars(bin2dec(binstr)))
                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)
                Else
                    'Return nothing if invalid char is detected
                    Return Nothing
                End If
            Next
            
            '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)
                Else
                	binstr = binstr.PadRight(8, "0"c)
                End If
                bytes.Add(CType(bin2dec(binstr), Byte))
                index += 8
            End While
            Return bytes.ToArray
        End Function
        
    	'Usage
    	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