Skip to content

Instantly share code, notes, and snippets.

@JohnMcLear
Last active March 14, 2024 14:00
Show Gist options
  • Save JohnMcLear/f8d08262023f472624264fa8e81c175b to your computer and use it in GitHub Desktop.
Save JohnMcLear/f8d08262023f472624264fa8e81c175b to your computer and use it in GitHub Desktop.
Convert Paxton
const convertPaxton = (uid) => {
if (!uid) uid = "11223344"
const type = 3;
let result = 0n;
let dec = 0n;
// For each digit in 11223344
uid.split('').forEach(
(digit, index) => {
dec = (getParity(BigInt(digit)) << 4n) + BigInt(digit); // getParity does some funky shit and returns the new BigInt.
// 4n is Numeric 4 but represented in a way that it can iterate with BitWise operators
// Attempting to represent this as Numeric 4 yields error TypeError: Cannot mix BigInt and other types, use explicit conversions
// But essentially here it's just an Numeric value of 4.
result = (result << 5n) + BigInt(dec);
// if it's the 6th item in 11223344 (so "3") then the result is the result with a left shift of 2.
if (index == 5) {
result = result << 2n;
}
}
);
// The result is a Math operator of the result left shifted by 22 then added to 412.... and then added to 3.
result = (result << 22n) + 4128768n + BigInt(type);
// each page value is a substring of this result
const page4 = result.toString(16).substr(0, 8);
const page5 = result.toString(16).substr(8, 16);
console.log(page4, page5);
}
const getParity = (n) => {
return ~(((BigInt(n) * BigInt("0x0101010101010101")) & BigInt("0x8040201008040201")) % BigInt("0x1FF")) & 1n;
// ~ is a bitwise operator - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operator
// & 1n the bitwise AND operator - This operator expects two numbers and retuns a number.
// In case they are not numbers, they are cast to numbers.
// % is a modulo which is similar to 14/5 = 4 as 4 is the left over after dividing wholes.
}
// Get the ID from the command line input IE node convertPaxton.js 11223344
const id = process.argv[2];
convertPaxton(id);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment