Base64 Encode and Decode using Kotlin (Android)

Base64 class

package com.example.app

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Base64
import android.util.Log

class MainActivity : AppCompatActivity()
{
    override fun onCreate(savedInstanceState: Bundle?)
    {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        var text = "Hello"
        var data = text.toByteArray()
        val base64Str = Base64.encodeToString(data, Base64.DEFAULT)
        Log.d("MY_APP", base64Str)

        data = Base64.decode(base64Str, Base64.DEFAULT)
        text = data.toString(Charsets.UTF_8)
        Log.d("MY_APP", text)
    }
}

Leave a Comment

Cancel reply

Your email address will not be published.