HEX to Base64 converter for JavaScript

The excellent comment by @dandavis is modified by StackOverflow, and has some weird hidden characters.

Here it is as one liner :

btoa("a6b580481008e60df9350de170b7e728".match(/\w{2}/g).map(function(a){return String.fromCharCode(parseInt(a, 16));} ).join(""))

or :

function hexToBase64(hexstring) {
    return btoa(hexstring.match(/\w{2}/g).map(function(a) {
        return String.fromCharCode(parseInt(a, 16));
    }).join(""));
}

hexToBase64("a6b580481008e60df9350de170b7e728");

Both return :

"prWASBAI5g35NQ3hcLfnKA=="

Note that the hex string should have an even length :

hexToBase64("00");
// => "AA=="
hexToBase64("000");
// => "AA=="

If you're working in Node or using Browserify, you can use

var base64String = Buffer.from(hexString, 'hex').toString('base64')

or

var hexString = Buffer.from(base64String, 'base64').toString('hex')