Base64 encode and decode example code

后端 未结 12 828
遥遥无期
遥遥无期 2020-11-22 15:00

Does anyone know how to decode and encode a string in Base64 using the Base64. I am using the following code, but it\'s not working.

String source = \"passwo         


        
12条回答
  •  有刺的猬
    2020-11-22 15:43

    For Kotlin mb better to use this:

    fun String.decode(): String {
        return Base64.decode(this, Base64.DEFAULT).toString(charset("UTF-8"))
    }
    
    fun String.encode(): String {
        return Base64.encodeToString(this.toByteArray(charset("UTF-8")), Base64.DEFAULT)
    }
    

    Example:

    Log.d("LOGIN", "TEST")
    Log.d("LOGIN", "TEST".encode())
    Log.d("LOGIN", "TEST".encode().decode())
    

提交回复
热议问题