Skip to content

Instantly share code, notes, and snippets.

@DefinitelyMaybe
Created August 15, 2023 03:57
Show Gist options
  • Save DefinitelyMaybe/4c68162acf7f82ecb352feb53b7f77f5 to your computer and use it in GitHub Desktop.
Save DefinitelyMaybe/4c68162acf7f82ecb352feb53b7f77f5 to your computer and use it in GitHub Desktop.
Test
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] & 0b11111000) << 8) | inputArray[1];
return {
flags: first3Bits.toString(16),
pid: next13Bits.toString(16),
};
}
try {
const fileName = "test_success.ts";
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) => {
console.log(`0x${pid}`);
});
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