将表示为字符串的十进制数转换为十六进制格式,仍然表示为字符串
Convert a decimal number represented as a string to its hex format still representing it as a string
我正试图使用bigInteger.js库在160位整数中进行运算,但我想保留这些整数的十六进制格式表示,这样我就可以传输它们并将它们用作ID。
var git_sha1 = require('git-sha1');
var bigInt = require("big-integer");
var uuid = git_sha1((~~(Math.random() * 1e9)).toString(36) + Date.now());
console.log('in hex 't', uuid); // See the uuid I have
console.log('in dec 't', bigInt(uuid, 16).toString()); // convert it to bigInt and then represent it as a string
console.log('to hex 't', bigInt(uuid, 16).toString(16)); // try to convert it back to hex
这是我的输出:
in hex 4044654fce69424a651af2825b37124c25094658
in dec 366900685503779409298642816707647664013657589336
to hex 366900685503779409298642816707647664013657589336
我需要to hex
与in hex
相同。有什么建议吗?非常感谢。
这是通过https://github.com/peterolson/BigInteger.js/pull/18
我不知道你是否如此依恋big-integer
,但如果你不是,bigint会做你想要的事情。
编辑:如果你想保持大整数,这应该做的技巧:
function toHexString(bigInt) {
var output = '';
var divmod;
while(bigInt.notEquals(0)) {
divmod = bigInt.divmod(16);
bigInt = divmod.quotient;
if (divmod.remainder >= 10)
output = String.fromCharCode(87+divmod.remainder) + output;
else
output = divmod.remainder + output;
}
return output;
}
相关文章:
- 重写Javascript's to字符串表示数字
- 从正则表达式文本中获取正则表达式字符串表示的最佳方式
- 如何在JavaScript中获取字符串表示的参数数组
- 给定两个整数的字符串表示形式,返回这些整数之和的字符串表示形式
- 如何确定字符串表示 Node.js 中的目录或文件
- 从 IEEE 754 单精度(32 位)浮点(字符串表示)创建字节
- 获取任何 JavaScript 值或对象的字符串表示形式的安全方法
- 如何在javascript中将json objectId转换为其字符串表示
- 获取构造函数函数的最佳方式是在JavaScript中使用带名称空间的字符串表示
- 如何将数组的字符串表示形式转换回数组
- JavaScript:将十六进制值的字符串表示形式转换为十六进制
- 我可以依靠 ES6 “符号”的字符串表示形式吗?
- 表示正则表达式字符串表示法
- 获取对象的字符串表示的不同方法
- 是否存在一种默认机制来获取XML Node的平面字符串表示形式
- 我如何子类化布尔来改变字符串表示,而不会在我的函数之外产生负面影响
- 获取JavaScript对象的字符串表示形式
- 将别名查询字符串表示为参数
- 用字符串表示的调用方法
- JSON字符串表示一个集合