Built and signed on GitHub ActionsBuilt and signed on GitHub Actions
Built and signed on GitHub Actions
latest
halvardssm/deno_stdextExtends @std/encoding. Functionalities includes hexdump
This package works with Cloudflare Workers, Node.js, Deno, Bun, Browsers
JSR Score
100%
Published
5 months ago (0.0.5)
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364/** * Utilities for hex encoded data. * @module * * ```ts * import { dump } from "@stdext/encoding/hex"; * const buffer = new TextEncoder().encode("Hello world!"); * console.log(dump(buffer)); * ``` */ /** * Convert a buffer to a hexdump string. * * @example * ```ts * const buffer = new TextEncoder().encode("The quick brown fox jumps over the lazy dog."); * console.log(dump(buffer)); * // 00000000 54 68 65 20 71 75 69 63 6b 20 62 72 6f 77 6e 20 |The quick brown | * // 00000010 66 6f 78 20 6a 75 6d 70 73 20 6f 76 65 72 20 74 |fox jumps over t| * // 00000020 68 65 20 6c 61 7a 79 20 64 6f 67 2e |he lazy dog.| * ``` */ export function dump(bufferView: ArrayBufferView | ArrayBuffer): string { let bytes: Uint8Array; if (ArrayBuffer.isView(bufferView)) { bytes = new Uint8Array(bufferView.buffer); } else { bytes = new Uint8Array(bufferView); } const lines = []; for (let i = 0; i < bytes.length; i += 16) { const address = i.toString(16).padStart(8, "0"); const block = bytes.slice(i, i + 16); const hexArray = []; const asciiArray = []; let padding = ""; for (const value of block) { hexArray.push(value.toString(16).padStart(2, "0")); asciiArray.push( value >= 0x20 && value < 0x7f ? String.fromCharCode(value) : ".", ); } if (hexArray.length < 16) { const space = 16 - hexArray.length; padding = " ".repeat(space * 2 + space + (hexArray.length < 9 ? 1 : 0)); } const hexString = hexArray.length > 8 ? hexArray.slice(0, 8).join(" ") + " " + hexArray.slice(8).join(" ") : hexArray.join(" "); const asciiString = asciiArray.join(""); const line = `${address} ${hexString} ${padding}|${asciiString}|`; lines.push(line); } return lines.join("\n"); }