Last active
October 3, 2024 07:44
-
-
Save kawaz/158f82c48bd0bb4f2b856c00c972d640 to your computer and use it in GitHub Desktop.
javascript string text(utf8) base64 hex tool
This file contains hidden or 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
| 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 <-> binary | |
| const htob = h => u8tob(htou8(h)) | |
| const btoh = b => u8toh(btou8(b)) | |
| // 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 { | |
| base64ToBase64Url: atoau, atoau, | |
| base64ToBinary: atob, atob, | |
| base64ToHexString: atoh, atoh, | |
| base64ToString: atos, atos, | |
| base64ToUint8Array: atou8, atou8, | |
| base64UrlToBase64: autoa, autoa, | |
| base64UrlToBinary: autob, autob, | |
| base64UrlToHexString: autoh, autoh, | |
| base64UrlToString: autos, autos, | |
| base64UrlToUint8Array: autou8, autou8, | |
| binaryToBase64: btoa, btoa, | |
| binaryToBase64Url: btoau, btoau, | |
| binaryToHexString: btoh, btoh, | |
| binaryToString: btos, btos, | |
| binaryToUint8Array: btou8, btou8, | |
| hexStringToBase64: htoa, htoa, | |
| hexStringToBase64Url: htoau, htoau, | |
| hexStringToBinary: htob, htob, | |
| hexStringToString: htos, htos, | |
| hexStringToUint8Array: htou8, htou8, | |
| stringToBase64: stoa, stoa, | |
| stringToBase64Url: stoau, stoau, | |
| stringToBinary: stob, stob, | |
| stringToHexString: stoh, stoh, | |
| stringToUint8Array: stou8, stou8, | |
| uint8ArrayToBase64: u8toa, u8toa, | |
| uint8ArrayToBase64Url: u8toau, u8toau, | |
| uint8ArrayToBinary: u8tob, u8tob, | |
| uint8ArrayToHexString: u8toh, u8toh, | |
| uint8ArrayToString: u8tos, u8tos, | |
| } | |
| })() | |
| 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