Add base 36
This commit is contained in:
parent
0d45c9b1de
commit
e813d3e5ba
@ -3,6 +3,9 @@ import { Component, Setter, createEffect, createSignal, on } from 'solid-js';
|
|||||||
import styles from './Page.module.css';
|
import styles from './Page.module.css';
|
||||||
import Page from './Page';
|
import Page from './Page';
|
||||||
|
|
||||||
|
const basicChars = '0123456789abcdefghijklmnopqrstuvwxyz';
|
||||||
|
// const base64Chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz01234567890+/';
|
||||||
|
|
||||||
function group(str: string, groupSize: number) {
|
function group(str: string, groupSize: number) {
|
||||||
const subs = [];
|
const subs = [];
|
||||||
for (let i = 0; i < Math.ceil(str.length / groupSize); i++) {
|
for (let i = 0; i < Math.ceil(str.length / groupSize); i++) {
|
||||||
@ -18,6 +21,32 @@ function parse(value: string, base: number) {
|
|||||||
return [...value].reduce((r, v) => r * BigInt(base) + BigInt(parseInt(v, base)), 0n);
|
return [...value].reduce((r, v) => r * BigInt(base) + BigInt(parseInt(v, base)), 0n);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*function parseBase64(value: string) {
|
||||||
|
return [...value].reduce((r, v) => r * BigInt(64) + BigInt(base64Chars.indexOf(v)), 0n);
|
||||||
|
}
|
||||||
|
|
||||||
|
function toBase64(value: bigint) {
|
||||||
|
let str = '';
|
||||||
|
while (value > 0) {
|
||||||
|
str = base64Chars[(Number(value % 64n))] + str;
|
||||||
|
value /= 64n;
|
||||||
|
}
|
||||||
|
return str;
|
||||||
|
}*/
|
||||||
|
|
||||||
|
function parseAscii(value: string) {
|
||||||
|
return [...value].reduce((r, v) => r * BigInt(256) + BigInt(v.charCodeAt(0)), 0n);
|
||||||
|
}
|
||||||
|
|
||||||
|
function toAscii(value: bigint) {
|
||||||
|
let str = '';
|
||||||
|
while (value > 0) {
|
||||||
|
str = String.fromCharCode(Number(value % 256n)) + str;
|
||||||
|
value /= 256n;
|
||||||
|
}
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
|
||||||
const ConvertBase: Component = () => {
|
const ConvertBase: Component = () => {
|
||||||
const [error, setError] = createSignal(' ');
|
const [error, setError] = createSignal(' ');
|
||||||
const [value, setValue] = createSignal(BigInt(0));
|
const [value, setValue] = createSignal(BigInt(0));
|
||||||
@ -28,41 +57,46 @@ const ConvertBase: Component = () => {
|
|||||||
const [octalTriplets, setOctalTriplet] = createSignal(true);
|
const [octalTriplets, setOctalTriplet] = createSignal(true);
|
||||||
const [hex, setHex] = createSignal('0');
|
const [hex, setHex] = createSignal('0');
|
||||||
const [hexPairs, setHexPairs] = createSignal(true);
|
const [hexPairs, setHexPairs] = createSignal(true);
|
||||||
|
const [base36, setBase36] = createSignal('0');
|
||||||
|
//const [base64, setBase64] = createSignal('');
|
||||||
|
const [ascii, setAscii] = createSignal('');
|
||||||
|
|
||||||
function updateExcept(value: string, base: number, except: Setter<string>) {
|
function updateExcept(value: string, base: number, except: Setter<string>) {
|
||||||
const raw = value.replaceAll(/ /g, '');
|
const raw = value.replaceAll(/ /g, '');
|
||||||
|
|
||||||
setError('');
|
setError('');
|
||||||
|
|
||||||
if (base == 2) { // TODO: common validation method
|
let num;
|
||||||
if ([...raw].find(c => !['0', '1'].includes(c))) {
|
if (base <= 36) {
|
||||||
setError('Invalid binary number');
|
if ([...raw].find(c => !basicChars.slice(0, base).includes(c) && !basicChars.toUpperCase().slice(0, base).includes(c))) {
|
||||||
|
setError('Invalid base ' + base + ' number');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
num = parse(raw, base);
|
||||||
|
}/* else if (base == 64) {
|
||||||
|
if ([...raw].find(c => !base64Chars.includes(c))) {
|
||||||
|
setError('Invalid base 64 string');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
num = parseBase64(raw);
|
||||||
|
}*/ else if (base == 256) {
|
||||||
|
num = parseAscii(raw);
|
||||||
|
} else {
|
||||||
|
setError('Invalid base');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
} else if (base == 8) {
|
|
||||||
if ([...raw].find(c => !['0', '1', '2', '3', '4', '5', '6', '7'].includes(c))) {
|
|
||||||
setError('Invalid octal number');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
} else if (base == 10) {
|
|
||||||
if ([...raw].find(c => !['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'].includes(c))) {
|
|
||||||
setError('Invalid decimal number');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
} else if (base == 16) {
|
|
||||||
if ([...raw].find(c => !['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'A', 'B', 'C', 'D', 'E', 'F'].includes(c))) {
|
|
||||||
setError('Invalid hex number');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const num = parse(raw, base);
|
|
||||||
|
|
||||||
setValue(num);
|
setValue(num);
|
||||||
if (setDecimal != except) setDecimal(num.toString());
|
if (setDecimal != except) setDecimal(num.toString());
|
||||||
if (setBinary != except) setBinary(binaryOctets() ? group(num.toString(2), 8) : num.toString(2));
|
if (setBinary != except) setBinary(binaryOctets() ? group(num.toString(2), 8) : num.toString(2));
|
||||||
if (setOctal != except) setOctal(octalTriplets() ? group(num.toString(8), 3) : num.toString(8));
|
if (setOctal != except) setOctal(octalTriplets() ? group(num.toString(8), 3) : num.toString(8));
|
||||||
if (setHex != except) setHex(hexPairs() ? group(num.toString(16), 2) : num.toString(16));
|
if (setHex != except) setHex(hexPairs() ? group(num.toString(16), 2) : num.toString(16));
|
||||||
|
if (setBase36 != except) setBase36(num.toString(36));
|
||||||
|
//if (setBase64 != except) setBase64(toBase64(num));
|
||||||
|
if (setAscii != except) setAscii(toAscii(num));
|
||||||
}
|
}
|
||||||
|
|
||||||
createEffect(on(binaryOctets, () => setBinary(binaryOctets() ? group(value().toString(2), 8) : value().toString(2))));
|
createEffect(on(binaryOctets, () => setBinary(binaryOctets() ? group(value().toString(2), 8) : value().toString(2))));
|
||||||
@ -92,6 +126,12 @@ const ConvertBase: Component = () => {
|
|||||||
<input id={'pairs'} type={'checkbox'} checked={hexPairs()} onchange={e => setHexPairs(e.currentTarget.checked)} />
|
<input id={'pairs'} type={'checkbox'} checked={hexPairs()} onchange={e => setHexPairs(e.currentTarget.checked)} />
|
||||||
<label for={'pairs'}>Pairs</label>
|
<label for={'pairs'}>Pairs</label>
|
||||||
</div>
|
</div>
|
||||||
|
<h2>Base 36</h2>
|
||||||
|
<textarea value={base36()} oninput={e => updateExcept(e.currentTarget.value, 36, setBase36)} />
|
||||||
|
{/*<h2>Base 64</h2>
|
||||||
|
<textarea value={base64()} oninput={e => updateExcept(e.currentTarget.value, 64, setBase64)} />*/}
|
||||||
|
<h2>ASCII</h2>
|
||||||
|
<textarea value={ascii()} oninput={e => updateExcept(e.currentTarget.value, 256, setAscii)} />
|
||||||
</Page >
|
</Page >
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user