Last active
March 15, 2024 09:39
-
-
Save thibmeu/d49746a77b09a65807801e92a50cbba4 to your computer and use it in GitHub Desktop.
Uint8Array helper for encoding and decoding, similar to Node.js `Buffer`
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
// Helper methods | |
const hex_decode = (s) => | |
Uint8Array.from(s.match(/.{1,2}/g).map((b) => parseInt(b, 16))); | |
const hex_encode = (u) => | |
Array.from(u).map((b) => b.toString(16).padStart(2, '0')).join('') | |
const ascii_decode = (s) => | |
Uint8Array.from(Array.from(s).map((l) => l.charCodeAt(0))) | |
const ascii_encode = (u) => | |
Array.from(u).map((l) => String.fromCharCode(l)).join('') | |
const u8ToB64 = (u) => | |
btoa(String.fromCharCode(...u)) | |
const b64Tou8 = (b) => | |
Uint8Array.from(atob(b), c => c.charCodeAt(0)) | |
const b64ToB64URL = (s) => | |
s.replace(/\+/g, '-').replace(/\//g, '_') | |
const b64URLtoB64 = (s) => | |
s.replace(/-/g, '+').replace(/_/g, '/') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment