summaryrefslogtreecommitdiff
path: root/src/utils/bases.ts
diff options
context:
space:
mode:
authorJoão Augusto Costa Branco Marado Torres <torres.dev@disroot.org>2025-06-24 12:08:41 -0300
committerJoão Augusto Costa Branco Marado Torres <torres.dev@disroot.org>2025-06-24 12:50:43 -0300
commitf9a77c5c27aede4e5978eb55d9b7af781b680a1d (patch)
treed545e325ba1ae756fc2eac66fac1001b6753c40d /src/utils/bases.ts
feat!: initial commit
Signed-off-by: João Augusto Costa Branco Marado Torres <torres.dev@disroot.org>
Diffstat (limited to 'src/utils/bases.ts')
-rw-r--r--src/utils/bases.ts11
1 files changed, 11 insertions, 0 deletions
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(
+ "",
+ );
+};