encode | decode | decimal

> bcd | decimal | binary <

// BCD - Binary-Coded Decimal for precise decimal arithmetic

0 chars
0 chars
[DECIMAL]

Decimal Precision

Preserves decimal accuracy avoiding binary rounding errors.

[4-BIT]

Nibble Encoding

Each decimal digit encoded in exactly 4 bits (one nibble).

[VARIANTS]

Multiple Formats

Support for 8421, 2421, 5421, Excess-3, and Gray BCD.

>> technical info

How BCD Works:

BCD encodes each decimal digit (0-9) into 4 binary bits. Different BCD formats use different bit weightings: 8421 (standard), 2421 (self-complementing), 5421 (bi-quinary), Excess-3 (adds 3 to each digit), and Gray BCD (minimizes bit transitions). Packed BCD stores two digits per byte for efficiency.

BCD Formats Example:

Decimal: 9 5 3

8421 BCD:  1001 0101 0011
2421 BCD:  1111 1011 0011
5421 BCD:  1100 1000 0011
Excess-3:  1100 1000 0110
Gray BCD:  1101 0111 0010

Packed:    10010101 00110000
           (95)     (30)

Why Use BCD:

  • >Financial calculations requiring exact decimal representation
  • >Digital displays and seven-segment decoders
  • >Real-time clock circuits
  • >Calculator and computing chips
  • >PLC and industrial control systems

>> frequently asked questions

What is BCD?

Binary-Coded Decimal (BCD) is a binary encoding of decimal numbers where each decimal digit is represented by its 4-bit binary equivalent. It's used when decimal accuracy is crucial.

Why use BCD instead of binary?

BCD avoids rounding errors in decimal calculations and simplifies conversion to decimal displays. It's perfect for financial applications where exact decimal representation is required.

What is packed BCD?

Packed BCD stores two decimal digits in one byte (8 bits), with each nibble representing one digit. It's more space-efficient than unpacked BCD which uses a full byte per digit.

What are the different BCD formats?

Common BCD formats include: 8421 (standard weighted), 2421 (self-complementing), 5421 (bi-quinary), Excess-3 (each digit +3), and Gray BCD (minimizes bit changes between successive values).

BCD 码是什么?和二进制有什么区别?

BCD(Binary-Coded Decimal,二进制编码的十进制)是一种用 4 个二进制位表示一个十进制数字 0–9 的编码方式,又叫 BCD 码。例如十进制 958421 BCD 表示为 1001 0101(不是纯二进制 01011111)。BCD 让十进制精确存储和显示,不存在像 0.1 + 0.2 = 0.30000000000000004 这样的二进制浮点误差,因此广泛用于计算器、数字钟、收款机、POS、PLC 工控系统等场合。

BCD 码如何与十进制 / 二进制相互转换?

十进制 → BCD:把每一位十进制数字单独编码成 4 位二进制。例 12580001 0010 0101 1000
BCD → 十进制:每 4 位一组读回 0–9。若某组 > 9(即 1010–1111)则该 BCD 非法。
BCD ↔ 二进制:通常要经过十进制中转——先把 BCD 当作十进制读出,再转成纯二进制。本工具的 [ANALYZE] 会同时给出十进制、二进制和多种 BCD 变体对照,方便调试。

BCD 编码常见的变体有哪些?

同一个十进制数字在不同 BCD 编码下位模式不同:
8421 BCD(标准 BCD)——最常用,位权 8-4-2-1,0=0000、9=1001。
2421 BCD(Aiken 码)——自补码,9 是 0 的 1-的补码。
5421 BCD——双五码,位权 5-4-2-1,便于十进制计数器级联。
余 3 码(Excess-3)——每个数字 +3 后再用 8421 编码,便于 BCD 加法器处理进位。
格雷 BCD——相邻数字只改变 1 位,用于机械编码盘和旋转编码器。
打包 BCD(Packed BCD)——一字节塞两位十进制,节省 50% 存储。

BCD 編碼(BCD 變換 / BCD codiert / BCD encodage)在哪里用?

多语环境下对 BCD 编码(BCD 編碼BCD 変換BCD codiertBCD encodageBCD format)的主要用途是一致的:
七段数码管驱动 IC(如 74LS47、CD4511)直接接收 4 位 BCD 输入。
PLC 与工业控制——西门子 S7、三菱 FX 的 TIME / DATE 寄存器就是 BCD。
ISO 7813 磁条卡——track 2 上的 PAN、到期日都是 BCD 压缩。
IBM S/390 / z/Architecture——Packed Decimal 指令直接在 BCD 上做加减乘除。
RTC 实时时钟芯片——DS1307 / PCF8563 的秒、分、时寄存器都是 BCD。

2 位 BCD 怎么转 8 位二进制?

2 位十进制(即 2 个 BCD 数字)代表范围 00–99,需要至少 7 位二进制(最大 99 = 1100011)。实际硬件一般用 8 位二进制寄存器存。转换公式:
binary = BCD_high × 10 + BCD_low
例:BCD 1001 0101(95)→ 9 × 10 + 5 = 95 = 0101 1111(8 位二进制)。本工具在 ANALYZE 模式会自动给出并列输出。

BCD 编码与各种进制的区别和互转?

简要对比:
BCD 编码:每个十进制数字独立编码,位宽固定,不是真正的基数表示;适合显示与精确十进制运算。
纯二进制:把整个数作为基数 2 表示,最紧凑,CPU 原生支持。
十六进制:基数 16,每 4 位打包一位十六进制数字,便于人类阅读二进制。
Base32 / Base64:用于把二进制字节显示成文本,字符集不同。
相互转换:BCD ↔ 十进制是一对一的位-数字映射;十进制 ↔ 二进制 ↔ 十六进制走数值转换。对比演示见 /number-base-converter/

如何判断一段二进制是不是合法 BCD 码?

以 4 位为一组查看:每组的值必须在 0000–1001(即 0–9)之间。任何 1010–1111(十进制 10–15)都是非法 BCD(8421 变体下)。额外长度约束:BCD 流的长度必须是 4 的倍数。我们的工具在 [FROM BCD] 时会自动检查并报错。部分硬件在非法组出现时会触发中断或显示 E 符号。

Other Languages