Convert Gray Code to Decimal

Decode Gray code (reflected binary) to decimal, binary, or hex using BigInt. Per-line errors, XOR step-trace, 10,000-row batches. Free, offline, 100% client-side.

Decode reflected binary (Gray code) into decimal - via the intermediate Gray → Binary XOR chain, then base-2 → base-10. Accepts 0b prefixes, spaces, and underscore separators. BigInt-safe for any bit width.

Enter Gray code to convert.

How to Use Convert Gray Code to Decimal

  1. Paste your Gray code values, one per line. You can include a 0b prefix, spaces, or underscores (1011_0110); the tool strips all three before decoding.
  2. Choose the output format: decimal (default), the intermediate binary representation, hexadecimal, or all four columns side by side for spreadsheet paste.
  3. Press Convert (or Ctrl+Enter / Cmd+Enter). Auto-convert also fires 200 ms after your last keystroke, so typing is immediately reflected.
  4. Read the stats line: it shows total lines, how many converted, how many had errors, the largest bit width seen, and the largest decimal produced.
  5. Inspect the breakdown: every decoded line appears with its Gray → Binary → Decimal chain and the matching hex in parentheses. Toggle "Show XOR step trace" to expose the bit-by-bit XOR chain too.
  6. Deal with errors per line: a bad character shows a red row naming the exact position (e.g., "Line 3: invalid character '2' at position 2") while every other row still converts.
  7. Copy or download. Copy uses the Clipboard API with an execCommand fallback; Download saves the output as a plain .txt file.

Frequently Asked Questions

How is Gray code decoded to decimal?

In two stages. Stage one converts Gray code to standard binary: the MSB stays the same, and each subsequent bit is the XOR of the previous binary bit with the current Gray bit (binary[i] = binary[i-1] ⊕ gray[i]). Stage two reads that binary as an unsigned base-10 integer. Gray 1011 → binary 1101 → decimal 13.

Does this tool handle Gray codes larger than 2^53?

Yes. The decimal output uses JavaScript’s native BigInt, which has no fixed upper bound. A 256-bit or 1,024-bit Gray code produces its exact decimal value – something ordinary Number arithmetic would silently round off beyond 9,007,199,254,740,991 (2^53 − 1).

What formats can the output be displayed in?

Four: decimal (default), binary (the intermediate stage of the decode), hexadecimal with 0x prefix, and “all four” which prints Gray / Binary / Decimal / Hex tab-separated per line – ideal for dropping straight into Excel, Google Sheets, or a CSV.

What input formats does the tool accept?

Plain binary strings like 1011, strings with a 0b prefix (0b1011), and values with visual separators like spaces (1011 0110) or underscores (1011_0110). All get normalized to pure digit strings before decoding. Blank lines are skipped silently.

What happens if one line in my batch is invalid?

Only that line fails. The tool walks the input line-by-line, so a rogue character such as 1021 becomes one red row (“Line 3: invalid character ‘2’ at position 2”) while every other line still decodes. The stats row shows how many succeeded versus how many failed.

Is this practical for rotary encoder output?

Yes – that’s the canonical use case. Absolute rotary encoders and shaft-position sensors emit Gray code precisely because only one bit changes between adjacent positions, eliminating transient glitches during transitions. Paste the raw encoder reading here to recover the integer shaft position.

How is my data protected?

Every byte of decoding happens in your browser’s JavaScript runtime – nothing is uploaded, logged, or retained by any server. You can verify in your browser’s Network tab that no outbound requests fire during a conversion. The page even keeps working after you disconnect from the internet.

How do I go the other direction (decimal to Gray code)?

Use the companion decimal-to-Gray-code converter. The round trip is lossless: encode a decimal to Gray, decode it back here, and you get the original decimal. The formulas are inverses (gray[i] = bin[i] ⊕ bin[i-1] forward; this tool’s XOR chain backward).

What does the “XOR step trace” checkbox do?

It prints each bit of the Gray → binary derivation below the decoded row, such as bit 1: keep 1 → 1 · bit 2: 1⊕1 → 0 · bit 3: 0⊕1 → 1. It’s the quickest way to verify a homework answer or teach a student why reflected binary decodes left-to-right.

How fast is this on large batches?

The decoder is O(n) in total bit length and the BigInt conversion is O(digits). 10,000 lines of 128-bit Gray code finish under 200 ms on a modern laptop. The breakdown panel caps at 50 displayed rows for layout performance, but the output textarea always contains the full result.