【问题标题】:What is the best way to convert a Buffer to a custom base?将缓冲区转换为自定义基础的最佳方法是什么?
【发布时间】:2020-02-26 09:34:35
【问题描述】:

如何在自定义库中输出 Buffer 的字符串表示形式? (即八进制,base26,base58,base64,...)

我惊讶地发现,在没有像 BN 这样的外部库(最多只使用 NodeJS 原生库)的情况下重新编码特别具有挑战性

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));

【问题讨论】:

标签: javascript node.js buffer base


【解决方案1】:

您可以尝试使用iconv-lite

wiki 中解释了如何创建新的自定义编码。

【讨论】:

  • 我的问题不是关于编码,而是关于数字基数
猜你喜欢
  • 2016-05-29
  • 2012-02-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多