From f9a77c5c27aede4e5978eb55d9b7af781b680a1d Mon Sep 17 00:00:00 2001 From: João Augusto Costa Branco Marado Torres Date: Tue, 24 Jun 2025 12:08:41 -0300 Subject: feat!: initial commit MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: João Augusto Costa Branco Marado Torres --- src/utils/bases.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 src/utils/bases.ts (limited to 'src/utils/bases.ts') diff --git a/src/utils/bases.ts b/src/utils/bases.ts new file mode 100644 index 0000000..a610d13 --- /dev/null +++ b/src/utils/bases.ts @@ -0,0 +1,11 @@ +export const bufferToBase = (buf: Uint8Array, base = 10): string => { + if (base < 2 || base > 36) { + throw new RangeError("Base must be between 2 and 36."); + } + + const max = Math.ceil(8 / Math.log2(base)); // Math.log2(1 << 8) = 8 + + return Array.from(buf, (byte) => byte.toString(base).padStart(max, "0")).join( + "", + ); +}; -- cgit v1.2.3