我正在尝试使用整数数组在JavaScript中实现BigInt类型。现在每个整数的上限为256。我已经完成了所有整数操作,但我无法弄清楚如何将BigInt转换为其字符串表示形式。当然,简单的方法是:
BigInt.prototype.toString = function(base) {
var s = '', total = 0, i, conv = [
,,
'01',
'012',
'0123',
'01234',
'012345',
'0123456',
'01234567',
'012345678',
'0123456789',
,
,
,
,
,
'0123456789abcdef'
];
base = base || 10;
for(i = this.bytes.length - 1; i >= 0; i--) {
total += this.bytes[i] * Math.pow(BigInt.ByteMax, this.bytes.length - 1 - i);
}
while(total) {
s = conv[base].charAt(total % base) + s;
total = Math.floor(total / base);
}
return s || '0';
};
但是当BigInts实际变得很大时,我将无法再通过加法进行转换。我该如何将基于x的数组转换为基于y的数组?
Math.pow
并将其相加,然后进行转换,我将超出JavaScript Number允许的大小,并最终得到Infinity
。 - Ry-