diff --git a/src/tools/integer-base-converter/integer-base-converter.model.ts b/src/tools/integer-base-converter/integer-base-converter.model.ts index 51cfab17..86274563 100644 --- a/src/tools/integer-base-converter/integer-base-converter.model.ts +++ b/src/tools/integer-base-converter/integer-base-converter.model.ts @@ -2,6 +2,48 @@ export function hasNumberPrefix(value: string) { return (value ?? '').trim().match(/^(0[xob].|&[hob].)/i); } +export function convertBase( + { + value, fromBase, toBase, + ignorePunctuationsRegexChars = ' \u00A0_\.,-', + handlePrefixSuffix = true, + ignoreCase = true, + }: { + value: string + fromBase: number + toBase: number + ignorePunctuationsRegexChars?: string + handlePrefixSuffix?: boolean + ignoreCase?: boolean + }) { + let cleanedValue = (value ?? '0').trim(); + if (ignorePunctuationsRegexChars) { + cleanedValue = cleanedValue.replace(new RegExp(`[${ignorePunctuationsRegexChars}]`, 'g'), ''); + } + let finalFromBase = fromBase; + if (handlePrefixSuffix) { + for (const regBase of [ + { base: 2, regex: /^(&b|0b)?([01]+)([IULZn]*)$/i }, + { base: 8, regex: /^(&o|0o)?([0-7]+)([IULZn]*)$/i }, + { base: 16, regex: /^(&h|0x)?([a-f0-9]+)([IULZn]*)$/i }, + ]) { + const match = cleanedValue.match(regBase.regex); + if (match) { + if (match[1]) { + finalFromBase = regBase.base; + } + cleanedValue = match[2]; + break; + } + } + } + if (ignoreCase && finalFromBase <= 36) { + cleanedValue = cleanedValue.toLowerCase(); + } +export function hasNumberPrefix(value: string) { + return (value ?? '').trim().match(/^(0[xob].|&[hob].)/i); +} + export function convertBase( { value, fromBase, toBase, @@ -42,13 +84,17 @@ export function convertBase( } const range = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/'.split(''); const fromRange = range.slice(0, finalFromBase); + const fromRange = range.slice(0, finalFromBase); const toRange = range.slice(0, toBase); + let decValue = cleanedValue let decValue = cleanedValue .split('') .reverse() + .reduce((carry: bigint, digit: string, index: number) => { .reduce((carry: bigint, digit: string, index: number) => { if (!fromRange.includes(digit)) { throw new Error(`Invalid digit "${digit}" for base ${finalFromBase}.`); + throw new Error(`Invalid digit "${digit}" for base ${finalFromBase}.`); } return (carry += BigInt(fromRange.indexOf(digit)) * BigInt(fromBase) ** BigInt(index)); }, 0n); @@ -56,6 +102,8 @@ export function convertBase( while (decValue > 0) { newValue = toRange[Number(decValue % BigInt(toBase))] + newValue; decValue = (decValue - (decValue % BigInt(toBase))) / BigInt(toBase); + newValue = toRange[Number(decValue % BigInt(toBase))] + newValue; + decValue = (decValue - (decValue % BigInt(toBase))) / BigInt(toBase); } return newValue || '0'; } diff --git a/src/tools/integer-base-converter/integer-base-converter.vue b/src/tools/integer-base-converter/integer-base-converter.vue index 2445baa5..e2a3478d 100644 --- a/src/tools/integer-base-converter/integer-base-converter.vue +++ b/src/tools/integer-base-converter/integer-base-converter.vue @@ -1,6 +1,6 @@