Skip to content

Instantly share code, notes, and snippets.

@calebkiage
Created January 31, 2021 23:13
Show Gist options
  • Save calebkiage/51ed2f44a47ee6b87ef938e8ee2a2341 to your computer and use it in GitHub Desktop.
Save calebkiage/51ed2f44a47ee6b87ef938e8ee2a2341 to your computer and use it in GitHub Desktop.
Converts ASCII numbers to Integers using Logical AND
const buffer = Buffer.from("9999999999", "ascii");
const mask = 15; // 00001111
let result = 0;
for (let i = 0; i < buffer.length; i++) {
const place = 1 * (10 ** i);
const pos = (buffer.length - i) - 1;
result += (buffer[pos] & mask) * place;
}
console.log(result);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment