diff options
author | Louis Pilfold <louis@lpil.uk> | 2023-10-26 13:03:22 +0100 |
---|---|---|
committer | Louis Pilfold <louis@lpil.uk> | 2023-10-26 13:07:39 +0100 |
commit | e0cefe61b7519d8356885b941c34bbf4763cb71c (patch) | |
tree | c6dbff1309805ecb6f1b0d9df6cb091552ec6970 /src/gleam_stdlib.mjs | |
parent | d55bfcbfc6c6c836013e4b9b45eb70c729d5ca7b (diff) | |
download | gleam_stdlib-e0cefe61b7519d8356885b941c34bbf4763cb71c.tar.gz gleam_stdlib-e0cefe61b7519d8356885b941c34bbf4763cb71c.zip |
Hex!
Diffstat (limited to 'src/gleam_stdlib.mjs')
-rw-r--r-- | src/gleam_stdlib.mjs | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/src/gleam_stdlib.mjs b/src/gleam_stdlib.mjs index 645e22a..2a1a6cf 100644 --- a/src/gleam_stdlib.mjs +++ b/src/gleam_stdlib.mjs @@ -842,3 +842,22 @@ export function inspectBitArray(bits) { export function inspectUtfCodepoint(codepoint) { return `//utfcodepoint(${String.fromCodePoint(codepoint.value)})`; } + +export function base16_encode(bit_array) { + let result = ""; + for (const byte of bit_array.buffer) { + result += byte.toString(16).padStart(2, "0").toUpperCase(); + } + return result; +} + +export function base16_decode(string) { + const bytes = new Uint8Array(string.length / 2); + for (let i = 0; i < string.length; i += 2) { + const a = parseInt(string[i], 16); + const b = parseInt(string[i + 1], 16); + if (isNaN(a) || isNaN(b)) return new Error(Nil); + bytes[i / 2] = a * 16 + b; + } + return new Ok(new BitArray(bytes)); +} |