"use client"; import { useState } from "react"; import { Calculator, Copy, Trash2 } from "lucide-react"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; import { Label } from "@/components/ui/label"; import { toast } from "sonner"; export default function RadixConverterPage() { const [values, setValues] = useState({ bin: "", oct: "", dec: "", hex: "" }); const handleUpdate = (val: string, radix: number) => { if (val === "") { setValues({ bin: "", oct: "", dec: "", hex: "" }); return; } try { const decimal = parseInt(val, radix); if (isNaN(decimal)) return; setValues({ bin: decimal.toString(2), oct: decimal.toString(8), dec: decimal.toString(10), hex: decimal.toString(16).toUpperCase() }); } catch { // Ignore invalid input } }; const copyToClipboard = async (text: string) => { if (!text) return; try { await navigator.clipboard.writeText(text); toast.success("已复制到剪贴板"); } catch { toast.error("复制失败"); } }; const clearAll = () => setValues({ bin: "", oct: "", dec: "", hex: "" }); return (
{/* Page Header */}

进制转换器

二、八、十、十六进制数值相互转换

转换面板
{/* Decimal */}
handleUpdate(e.target.value, 10)} className="font-mono text-lg" />
{/* Hexadecimal */}
handleUpdate(e.target.value, 16)} className="font-mono text-lg" />
{/* Binary */}
handleUpdate(e.target.value, 2)} className="font-mono text-lg" />
{/* Octal */}
handleUpdate(e.target.value, 8)} className="font-mono text-lg" />
{/* Info Card */} 💡 进制转换规则
  • 二进制 (Binary): 基数为2,由数字0和1组成。
  • 八进制 (Octal): 基数为8,由数字0到7组成。
  • 十进制 (Decimal): 基数为10,是我们日常生活中最常用的计数方式。
  • 十六进制 (Hexadecimal): 基数为16,由0-9和A-F(代表10-15)组成。
); }