Android Code Snippet Check if a string is Base64 (isBase64)

Douglas Farias

Expert
Licensed User
Hi all.
I found this example in some old folders on my pc and decided to share here.
it is a simple code to check if a string is base64 hash.

This example use a regex.

B4X:
Sub isBase64(text As String) As Boolean
    If Regex.IsMatch("^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)$", text.Trim) And text.Length > 3  Then
        Return True
    Else
        Return False
    End If
End Sub

Example of use
B4X:
    ' TESTA A STRING CODIFICADA COM BASE64
    Log(isBase64("ZG91Z2xhc2Zhcmlhcw==")) '=douglasfarias
 
    ' TESTA A STRING COM TEXTO
    Log(isBase64("Test string"))'=dont is a base64 string

The result log is
true
false

Credits: stackoverflow, Douglas Farias and Flavio Rocha
 

Attachments

Last edited:
Top