Created
January 24, 2018 16:42
-
-
Save EmmanuelGuther/b25ad5e46a78165a0bbd201d6ec73d65 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.util.Base64 | |
import java.io.UnsupportedEncodingException | |
/** | |
* @param message the message to be encoded | |
* | |
* @return the enooded from of the message | |
*/ | |
fun toBase64(message: String): String? { | |
val data: ByteArray | |
try { | |
data = message.toByteArray(charset("UTF-8")) | |
return Base64.encodeToString(data, Base64.DEFAULT) | |
} catch (e: UnsupportedEncodingException) { | |
e.printStackTrace() | |
} | |
return null | |
} | |
/** | |
* @param message the encoded message | |
* | |
* @return the decoded message | |
*/ | |
fun fromBase64(message: String?): String? { | |
val data = Base64.decode(message, Base64.DEFAULT) | |
try { | |
return String(data, charset("UTF-8")) | |
} catch (e: UnsupportedEncodingException) { | |
e.printStackTrace() | |
} | |
return null | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment