1

I have two long number that i represent those in JavaScript using BigInt ( because of the 53 bit integer length in JavaScipt instead of 64 bit). I found myself in the need to create out of those two long number an UUID / GUID in JavaScript.

In SO i was able to find the same question multiple times but always for different programming languages but not for JavaScript, example here:

Basically i am looking for something like the one in Java i.e. example here:

public UUID(long mostSigBits, long leastSigBits) {
    this.mostSigBits = mostSigBits;
    this.leastSigBits = leastSigBits;
}

and we can use it like:

UUID  tempUUID1 = new UUID(55, 100);

results in :

00000000-0000-0037-0000-000000000064

The approach that i am thinking to take so far is convert the decimal to hex like that

 BigInt("55").toString('16')     // results to 37
 BigInt("100").toString('16')    // results to 64

and then pad the missing zeros. How such function can be implemented, I am looking for an example.

Preferably using the WebCryptoAPI (unfortunately node.js is not what i am looking for here), that can create and read/split such UUID / GUID to 2 separate BigInt's i.e. the "mostSigBits" and the "leastSigBits" values.

I would really appreciate if someone provides an example of such function / method . Thank you in advanced.

Tito
  • 2,234
  • 6
  • 31
  • 65
  • *"...because of the 53 bit integer length in JavaScipt..."* It's IEEE-754 rather than JavaScript per se, and [rather more complicated than that](https://stackoverflow.com/questions/588004/is-floating-point-math-broken). :-) But yes, if they're really big numbers, BigInt is the way to go (in modern environments). – T.J. Crowder Sep 22 '20 at 07:07
  • Library recommendation questions are specifically off-topic for SO. See [*What types of questions should I avoid asking?*](/help/dont-ask), and [*What topics can I ask about here?*](/help/on-topic) – T.J. Crowder Sep 22 '20 at 07:09
  • well the point I was trying to make here is that those are 64 bit longs that can not be stored in 53 bit integer without losing data, and yes that is a good reading, thank you – Tito Sep 22 '20 at 07:09
  • [This answer](https://stackoverflow.com/questions/105034/how-to-create-guid-uuid) recommends a library, but yes, questions asking for a library recommendation is not recommended here. Usually [asking "how to do X" instead works](https://meta.stackoverflow.com/questions/379263/is-just-changing-which-library-to-do-into-how-to-do-already-enough). – user202729 Sep 22 '20 at 07:17
  • @user202729, thank you for the hint i changed that to code example, instead of a libarry – Tito Sep 22 '20 at 07:18
  • Do you need a valid UUID, and does those 2 numbers already represent a valid one? – user202729 Sep 22 '20 at 07:22
  • @user202729 those 2 numbers already represent a valid UUID, i just need a method to construct the UUID out of those 2 numbers and dissect an UUID to those 2 number using JavaScript BigInt – Tito Sep 22 '20 at 07:23

1 Answers1

2

There's no need for a library, formatting these numbers is fairly straightforward substring'ing:

function formatAsUUID(mostSigBits, leastSigBits) {
    let most = mostSigBits.toString("16").padStart(16, "0");
    let least = leastSigBits.toString("16").padStart(16, "0");
    return `${most.substring(0, 8)}-${most.substring(8, 12)}-${most.substring(12)}-${least.substring(0, 4)}-${least.substring(4)}`;
}

function formatAsUUID(mostSigBits, leastSigBits) {
    let most = mostSigBits.toString("16").padStart(16, "0");
    let least = leastSigBits.toString("16").padStart(16, "0");
    return `${most.substring(0, 8)}-${most.substring(8, 12)}-${most.substring(12)}-${least.substring(0, 4)}-${least.substring(4)}`;
}


const expect = "00000000-0000-0037-0000-000000000064";
const result = formatAsUUID(BigInt("55"), BigInt("100"));
console.log(result, expect === result ? "OK" : "<== Error");
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • T.J. Crowder , many thanks , this is exactlly what i was looking for. Can you please post also the reverse side i.e. from UUID back to mostSigBits and leastSigBits – Tito Sep 22 '20 at 08:35
  • @Tito - That's even simpler, use `substring` to get each half, remove the `-`, then use `BigInt` to parse the result. E.g., for `mostSigBits`: `BigInt(str.substring(0, 18).replace(/-/g, ""))` – T.J. Crowder Sep 22 '20 at 09:24