'use client' import { useState } from 'react' export default function NumberConverter() { const [value, setValue] = useState('') const [fromBase, setFromBase] = useState('10') const [error, setError] = useState('') const isValidNumber = (num: string, base: string) => { const validChars = { '2': /^[01]+$/, '10': /^[0-9]+$/, '16': /^[0-9A-Fa-f]+$/ } return validChars[base as keyof typeof validChars].test(num) } const convert = (input: string, from: string) => { if (!input) return { binary: '', decimal: '', hex: '' } try { if (!isValidNumber(input, from)) { throw new Error('Invalid number for selected base') } const decimal = parseInt(input, parseInt(from)) if (isNaN(decimal)) throw new Error('Invalid number') return { binary: decimal.toString(2), decimal: decimal.toString(10), hex: decimal.toString(16).toUpperCase() } } catch (err) { setError(err instanceof Error ? err.message : 'Invalid input') return { binary: '', decimal: '', hex: '' } } } const handleInputChange = (e: React.ChangeEvent) => { setValue(e.target.value) setError('') } const results = convert(value, fromBase) return (

Number Converter

Convert numbers between binary (base 2), decimal (base 10), and hexadecimal (base 16) formats. Enter a number in any base and see its equivalent representations.

{error && (
{error}
)}

Results

) }