Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,845 questions

51,766 answers

573 users

How to use Base64 to encode and decode a string in Kotlin

1 Answer

0 votes
import java.util.Base64

fun base64Encode(str: String): String {
    val bytes = str.toByteArray(Charsets.UTF_8)
    
    return Base64.getEncoder().encodeToString(bytes)
}

fun base64Decode(encodedStr: String): String {
    val bytesArray = Base64.getDecoder().decode(encodedStr)
    
    return String(bytesArray, Charsets.US_ASCII)
}

fun main() {
    val base64EncodeStr = base64Encode("Kotlin programming")
    
    println(base64EncodeStr)
    println(base64Decode(base64EncodeStr))
}


 
 
/*
run:
   
S290bGluIHByb2dyYW1taW5n
Kotlin programming
   
*/

 



answered Nov 21, 2024 by avibootz

Related questions

1 answer 87 views
1 answer 85 views
2 answers 115 views
1 answer 111 views
1 answer 245 views
1 answer 156 views
...