I'm trying to do operations in 160 bit integers using the bigInteger.js library, but I want to keep a representation of those in hex format so I can transmit them over and use them as 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
Here is my output:
in hex 4044654fce69424a651af2825b37124c25094658
in dec 366900685503779409298642816707647664013657589336
to hex 366900685503779409298642816707647664013657589336
I need that to hex
to be the same as in hex
. Any suggestions? Thank you!
I don't know if you're that attached to big-integer
, but if you're not, bigint does exactly what you want.
EDIT : If you want to keep big-integer, this should do the trick :
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;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.