Skip to content

Instantly share code, notes, and snippets.

@e-mihaylin
Last active August 30, 2018 08:49
Show Gist options
  • Select an option

  • Save e-mihaylin/f2fd1564439d6c0b5f6457d2fd718a14 to your computer and use it in GitHub Desktop.

Select an option

Save e-mihaylin/f2fd1564439d6c0b5f6457d2fd718a14 to your computer and use it in GitHub Desktop.
const hexToBase64 = hex => Buffer.from(hex, 'hex').toString('base64');
const hex2bin = Object.assign( ["0000","0001","0010","0011","0100","0101","0110","0111","1000","1001"], { a:"1010", b:"1011", c:"1100", d:"1101", e:"1110", f:"1111" } ) ,
bin2base64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" ,
hexToBase64 = s => s.replace( /./g, c => hex2bin[c] ).replace( /.{1,6}/g, s => bin2base64[Number.parseInt(s.padEnd(6,0),2)] ) + "=".repeat(s.length%3) ;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment