Skip to content

Instantly share code, notes, and snippets.

@HenriqueSilverio
Created July 23, 2020 22:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HenriqueSilverio/27dc3653c98416edd7d0ca422b5c345d to your computer and use it in GitHub Desktop.
Save HenriqueSilverio/27dc3653c98416edd7d0ca422b5c345d to your computer and use it in GitHub Desktop.
// 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