Skip to content

Instantly share code, notes, and snippets.

@therightstuff
Last active January 3, 2023 14:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save therightstuff/9f83967b9c23354a27ed691a6b591b0c to your computer and use it in GitHub Desktop.
Save therightstuff/9f83967b9c23354a27ed691a6b591b0c 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)}`;
};
@Pitshopdirect
Copy link

How do i convert back to guid?

@mikepiven
Copy link

Yes, how convert it back?

@DamithaPerera
Copy link

How do i convert back to uuid?

@therightstuff
Copy link
Author

@Pitshopdirect @mikepiven @DamithaPerera I I've just updated the gist with an explanation for why the original method isn't reversible and added an improved method.

Please note that the improved method only works if the integer value can continue to be represented as a string (or a BigNumber object) because Javascript's numbers aren't large enough to support UUIDs.

Your mileage may vary.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment