From 353937e3a176fd86ed8c0f07a868cea750219760 Mon Sep 17 00:00:00 2001 From: "Felix W. Dekker" Date: Sun, 5 Apr 2020 13:03:44 +0200 Subject: [PATCH] Fix base64 encoding Fixes #9. --- index.html | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/index.html b/index.html index 29b6f98..e9144b9 100644 --- a/index.html +++ b/index.html @@ -55,7 +55,7 @@ MIT License. Source code available on git. -
v1.4.3
+
v1.4.4
@@ -132,6 +132,32 @@ } } + class Base64NumeralSystem extends NumeralSystem { + constructor(alphabet) { + super(64, alphabet, true); + } + + + decimalToBase(decimalNumber) { + const hex = decimalNumber.toString(16); + const b64 = Array.from(hex.padStart(hex.length + hex.length % 2)) + .reduce((result, value, index, array) => { + if (index % 2 === 0) result.push(array.slice(index, index + 2)); + return result; + }, []) + .map(pair => String.fromCharCode(parseInt(pair.join(""), 16))) + .join(""); + + return btoa(b64); + } + + baseToDecimal(baseString) { + const hex = Array.from(atob(baseString)) + .map(char => char.charCodeAt(0).toString(16).padStart(2, "0")).join(""); + return bigInt(hex, 16); + } + } + class Base64NumeralSystemInput extends NumeralSystemInput { // TODO Convert static methods to static properties once supported by Firefox static defaultAlphabet() { @@ -144,7 +170,7 @@ constructor(name) { - super(name, new NumeralSystem(64, Base64NumeralSystemInput.defaultAlphabet(), true)); + super(name, new Base64NumeralSystem(Base64NumeralSystemInput.defaultAlphabet())); this.dropdownLabel = document.createElement("label"); this.dropdownLabel.setAttribute("for", `${this.name}Dropdown`);