What is the best way to convert a Buffer to a custom base?

Viewed 427

How to output a string representation of a Buffer in a custom base? (ie. octal, base26, base58, base64, ...)

I surprised myself to find it particularly challenging to recode this without external libraries like BN (using only NodeJS native ones at most)

const buff = Buffer.from('a4fe800c32', 'hex');

// OK
console.log(buff.toString('base64'));

// Would like
console.log(buff.toString('base58'));

// Or
const customBase = ['0', '1', '2', 'a', '§', 'K'];
console.log(buff.toString(customBase));
1 Answers

You could try using iconv-lite.

In the wiki it's explained how to create a new custom encoding.

Related