Skip to content

Instantly share code, notes, and snippets.

@kawaz
Last active April 14, 2024 10:57
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 kawaz/158f82c48bd0bb4f2b856c00c972d640 to your computer and use it in GitHub Desktop.
Save kawaz/158f82c48bd0bb4f2b856c00c972d640 to your computer and use it in GitHub Desktop.
javascript string text(utf8) base64 hex tool
const binconv = (()=>{
// base64 <-> binary
const atob = window.atob
const btoa = window.btoa
// binary <-> Uint8Array
const btou8 = b => new Uint8Array([].map.call(b,c=>c.charCodeAt(0))) //new Uint8Array([...b].map(c=>c.charCodeAt(0)))
const u8tob = u8 => String.fromCharCode.apply(null,u8) //String.fromCharCode(...u8)
// string <-> Uint8Array
const stou8 = s => new TextEncoder().encode(s)
const u8tos = u8 => new TextDecoder().decode(u8)
// base64 <-> Uint8Array
const atou8 = a => btou8(atob(a))
const u8toa = u8 => btoa(u8tob(u8))
// string <-> binary
const stob = s => u8tob(stou8(s))
const btos = b => u8tos(btou8(b))
// string <-> base64
const stoa = s => btoa(stob(s))
const atos = a => btos(atob(a))
// base64Url <-> base64
const autoa = a => a.replace(/-/g,'+').replace(/\//g,'/')+"==".substring(0,a%4%3)
const atoau = a => a.replace(/\+/g,'-').replace(/\//g,'_').replace(/=/g,'')
// base64Url <-> string
const autos = au => atos(autoa(au))
const stoau = s => atoau(stoa(s))
// base64Url <-> Uint8Array
const autou8 = a => atou8(autoa(a))
const u8toau = u8 => atoau(u8toa(u8))
// base64Url <-> binary
const autob = a => atob(autoa(a))
const btoau = b => atoau(btoa(b))
// hexString <-> Uint8Array
const u8toh = u8 => [].map.call(u8,i=>i.toString(16).padStart(2,'0')).join('')
const htou8 = h => new Uint8Array((normalizeHex(h).match(/../g)||[]).map(h=>parseInt(h,16)))
const normalizeHex = h => {const h2=h.replace(/\b0x/g,'').replace(/[^0-9a-f]/gi,'');return `${h2.length%2?'0':''}${h2}`}
// hexString <-> String
const stoh = s => u8toh(stou8(s))
const htos = h => u8tos(htou8(h))
// hexString <--> base64,base64url
const htoa = h => u8toa(htou8(h))
const htoau = h => u8toau(htou8(h))
const atoh = h => u8toh(atou8(h))
const autoh = h => u8toh(autou8(h))
// まとめ
return {
base64ToBinary: atob, atob,
binaryToBase64: btoa, btoa,
binaryToUint8Array: btou8, btou8,
uint8ArrayToBinary: u8tob, u8tob,
stringToUint8Array: stou8, stou8,
uint8ArrayToString: u8tos, u8tos,
stringToBinary: stob, stob,
binaryToString: btos, btos,
stringToBase64: stoa, stoa,
base64ToString: atos, atos,
base64UrlToBase64: autoa, autoa,
base64ToBase64Url: atoau, atoau,
base64UrlToString: autos, autos,
stringToBase64Url: stoau, stoau,
base64UrlToUint8Array: autou8, autou8,
uint8ArrayToBase64Url: u8toau, u8toau,
base64UrlToBinary: autob, autob,
binaryToBase64: btoau, btoau,
uint8ArrayToHexString: u8toh, u8toh,
hexStringToUint8Array: htou8, htou8,
stringToHexString: stoh, stoh,
hexStringToString: htos, htos,
hexStringToBase64: htoa, htoa,
hexStringToBase64Url: htoau, htoau,
Base64ToHexString: atoh, atoh,
Base64UrlToHexString: autoh, autoh,
}
})()
this.exports = binconv
//Object.keys(binconv).filter(k=>!this[k]).forEach(k=>this[k]=binconv[k])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment