Created
August 15, 2023 04:45
-
-
Save DefinitelyMaybe/05896a9c39f4a572f5af0ee348d0f19a to your computer and use it in GitHub Desktop.
Test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function uint8ArrayToHex(uint8Array) { | |
return Array.from(uint8Array) | |
.map((byte) => byte.toString(16).padStart(2, "0")) | |
.join(""); | |
} | |
function extractFlagAndPID(inputArray) { | |
const first3Bits = inputArray[0] & 0b111; | |
const next13Bits = ((inputArray[0] & 0b00011111) << 8) | inputArray[1]; | |
return { | |
flags: first3Bits.toString(16), | |
pid: next13Bits.toString(16).padStart(4, "0"), | |
}; | |
} | |
try { | |
const fileName = Deno.args[0]; | |
const fileContent = Deno.readFileSync(fileName); | |
const packetSize = 188; | |
let i = 0; | |
const uids = {}; | |
for (let index = 0; index < fileContent.length / packetSize; index++) { | |
i++; | |
const offset = index * 188; | |
const syncByte = uint8ArrayToHex(fileContent.slice(offset, offset + 1)); | |
if (syncByte != "47") { | |
throw `Error: No sync byte present in packet ${index}, offset ${offset}`; | |
} | |
const { flags, pid } = extractFlagAndPID( | |
fileContent.slice(offset + 1, offset + 3) | |
); | |
if (!(pid in uids)) { | |
uids[pid] = null; | |
} | |
} | |
[...Object.keys(uids)].sort().forEach((pid) => { | |
let x = pid.replace(/0+/, ""); | |
if (x.length == 0) { | |
x = "0"; | |
} | |
console.log(`0x${x}`); | |
}); | |
Deno.exit(0); | |
} catch (error) { | |
console.error(error); | |
Deno.exit(1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment