Skip to content

Instantly share code, notes, and snippets.

@lbngoc
Forked from therightstuff/explanation.js
Created December 3, 2024 02:01
Show Gist options
  • Save lbngoc/be260c98eae0063fcbdc6574bba8c4e2 to your computer and use it in GitHub Desktop.
Save lbngoc/be260c98eae0063fcbdc6574bba8c4e2 to your computer and use it in GitHub Desktop.
Convert GUID (UUID) to integer numbers in Javascript (and back again)
// A UUID is a 128-bit number which is too large to be represented as a native integer in Javascript.
// The original solution I posted (based on https://github.com/uuidjs/uuid#uuidparsestr ) wasn't good,
// as it converted the given UUID into a byte array and then selected only the first four bytes to be
// used as the integer.
const uuid = require('uuid');
let convertGuidToInt = (uuid) => {
// parse accountId into Uint8Array[16] variable
let parsedUuid = uuid.parse(uuid);
console.log(`uuid ${uuid} parsed successfully`);
// convert to integer - see answers to https://stackoverflow.com/q/39346517/2860309
let buffer = Buffer.from(parsedUuid);
console.log(`parsed uuid converted to buffer`);
let result = buffer.readUInt32BE(0);
console.log(`buffer converted to integer ${result} successfully`);
return result;
},
// The above example drops most of the number and so is a) impossible to convert back to UUID
// and b) unlikely to ensure uniqueness. In response to the requests for a way to convert the
// resulting integers back to UUIDs, here is the code to convert UUIDs to BigNumber objects
// and back again using the bignumber.js package:
const BigNumber = require('bignumber.js');
let convertGuidToInt = (id) => {
// remove the dashes from the given uuid and convert to a hexadecimal BigNumber object
const bn = new BigNumber(id.replace(/-/g, ''), 16);
// return the string representation of the BigNumber object as a decimal
return bn.toString(10);
};
let convertIntToGuid = (num) => {
// convert the string representation of the decimal number to a BigNumber object
const bn = new BigNumber(num, 10);
// convert the BigNumber to a hexadecimal string
const id = bn.toString(16);
// return the string with the dashes (8-4-4-4-12)
return `${id.substr(0, 8)}-${id.substr(8, 4)}-${id.substr(12, 4)}-${id.substr(16, 4)}-${id.substr(20)}`;
};
const BigNumber = require('bignumber.js');
let convertGuidToInt = (id) => {
// remove the dashes from the given uuid and convert to a hexadecimal BigNumber object
const bn = new BigNumber(id.replace(/-/g, ''), 16);
// return the decimal representation of the BigNumber object as a string
return bn.toString(10);
};
let convertIntToGuid = (num) => {
// convert the string representation of the decimal number to a BigNumber object
const bn = new BigNumber(num, 10);
// convert the BigNumber to a hexadecimal string
const id = bn.toString(16);
// return the string with the dashes (8-4-4-4-12)
return `${id.substr(0, 8)}-${id.substr(8, 4)}-${id.substr(12, 4)}-${id.substr(16, 4)}-${id.substr(20)}`;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment