Skip to content

Instantly share code, notes, and snippets.

@seventyeight
Created April 14, 2026 11:00
Show Gist options
  • Select an option

  • Save seventyeight/e8a1e9f7b3b1ec0958d2c3f5684a6931 to your computer and use it in GitHub Desktop.

Select an option

Save seventyeight/e8a1e9f7b3b1ec0958d2c3f5684a6931 to your computer and use it in GitHub Desktop.
Hexdump in TypeScript
import type { Buffer } from "node:buffer";
import { open } from "node:fs/promises";
import { argv, stdin, stdout } from "node:process";
function printChars(chars: number[]) {
stdout.write(" |");
chars
.map((c) => (c > 32 && c < 127 ? c : 46))
.map((c) => stdout.write(String.fromCodePoint(c)));
stdout.write("|\n");
}
function processStreamInput(chunk: Buffer) {
let memoryAdress = 0x0;
let codePoints = [];
for (let i = 0; i < chunk.length; i++) {
if (memoryAdress % 0x10 === 0x0) {
stdout.write(memoryAdress.toString(16).padStart(8, "0"));
stdout.write(" ");
}
const codePoint = Math.abs(chunk.readIntLE(i, 1));
codePoints[memoryAdress % 0x10] = codePoint;
stdout.write(codePoint.toString(16).padStart(2, "0"));
stdout.write(" ");
memoryAdress++;
if (memoryAdress % 0x8 === 0x0) {
stdout.write(" ");
}
if (memoryAdress % 0x10 === 0x0) {
printChars(codePoints);
codePoints = [];
}
}
const quantityRemainingBytes = 3 * (0x10 - (memoryAdress % 0x10));
stdout.write(
" ".repeat(quantityRemainingBytes + (quantityRemainingBytes < 0x8 ? 1 : 2))
);
printChars(codePoints);
}
if (argv.length > 2) {
const fd = await open(argv[2] as string);
for await (const chunk of fd.createReadStream()) {
processStreamInput(chunk);
}
} else {
for await (const chunk of stdin) {
processStreamInput(chunk);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment