I have this code to go from an array of bytes (arbitrarily long) to a "bigint" string (really, an arbitrarily long string composed of integers 0-9), and go from that long integer string into an array of bytes.
console.log(bigintStringToBytes('12321232123213213292948428924184'))
console.log(bytesToBigintString([ 155, 132, 12, 85, 200, 76, 41, 241, 14, 133, 71, 165, 24 ]))
function bigintStringToBytes(str) {
let dec = [...str], sum = []
while(dec.length){
let s = 1 * dec.shift()
for(let i = 0; s || i < sum.length; i++){
s += (sum[i] || 0) * 10
sum[i] = s % 256
s = (s - sum[i]) / 256
}
}
return Uint8Array.from(sum.reverse())
}
function bytesToBigintString(arr) {
var dec = [...arr], sum = []
while(dec.length){
let s = 1 * dec.shift()
for(let i = 0; s || i < sum.length; i++){
s += (sum[i] || 0) * 256
sum[i] = s % 10
s = (s - sum[i]) / 10
}
}
return sum.reverse().join('')
}
How can these two functions be optimized?
BigIntallowed? \$\endgroup\$