Created
July 23, 2020 22:27
-
-
Save HenriqueSilverio/27dc3653c98416edd7d0ca422b5c345d to your computer and use it in GitHub Desktop.
This file contains 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
// Implementation of Kotlin `String.toByteArray` in JavaScript | |
// https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/to-byte-array.html | |
const UTF8ByteArrayFromString = (string) => { | |
const encoder = new TextEncoder() | |
return encoder.encode(string) | |
} | |
const stringFromUTF8ByteArray = (array) => { | |
const decoder = new TextDecoder() | |
return decoder.decode(array) | |
} | |
const message = 'Hello' | |
const coded = UTF8ByteArrayFromString(message) | |
const decoded = stringFromUTF8ByteArray(coded) | |
console.log(coded) | |
console.log(decoded) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment