function NumericConverter() { const [decimalValue, setDecimalValue] = useState(''); const [customBase, setCustomBase] = useState('3'); const standardBases = [ { name: 'Binary', base: 2, icon: '0️⃣1️⃣' }, { name: 'Octal', base: 8, icon: '8️⃣' }, { name: 'Decimal', base: 10, icon: '🔟' }, { name: 'Hexadecimal', base: 16, icon: '🔠' }, ]; const extraBases = [ { name: 'Base 3', base: 3 }, { name: 'Base 5', base: 5 }, { name: 'Base 12', base: 12 }, { name: 'Base 20', base: 20 }, { name: 'Base 32', base: 32 }, { name: 'Base 36', base: 36 }, ]; const handleValueChange = (value, base) => { if (value === '') { setDecimalValue(''); return; } try { // Validate input for the base const validChars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ".substring(0, base); const regex = new RegExp(`^[${validChars}]*$`, "i"); if (!regex.test(value)) return; // Convert to decimal const dec = BigInt(parseInt(value, base)); // For display we'll use string representation setDecimalValue(dec.toString()); } catch (e) { // Handle large numbers or invalid inputs } }; const getValueForBase = (base) => { if (decimalValue === '') return ''; try { const num = parseInt(decimalValue, 10); if (isNaN(num)) return ''; return num.toString(base).toUpperCase(); } catch (e) { return 'Error'; } }; const clearAll = () => { setDecimalValue(''); }; return (
Convert between different bases in real-time. Supports bases 2 through 36.