Convert Gray Code to Hex

Decode Gray code (reflected binary) to hex, decimal, or binary. Accepts binary or hex-packed input. BigInt-safe, per-line errors. Free, offline, 100% client-side.

Decode reflected binary (Gray code) to hexadecimal, decimal, or standard binary. Accepts Gray as bits (1011) or as hex-packed bits (0xA = 1010). BigInt math for any width - 4, 64, 128, or 256 bits.

Enter Gray code to convert.

How to Use Convert Gray Code to Hex

  1. Paste your Gray code values, one per line. You can include a 0b/0x prefix, spaces, or underscores (1011_0110 or DE_AD) - they all get stripped before decoding.
  2. Pick the input mode: "Binary bits" (default) if each line is already in 0/1 form, or "Hex-packed Gray" if your Gray code is stored as compact hex (e.g. 0xA representing Gray bits 1010).
  3. Choose the output format: hex (default), decimal, the decoded binary bits, or all four columns tab-separated for pasting into a spreadsheet.
  4. Toggle UPPERCASE hex if your downstream system expects 0xAF over 0xaf.
  5. Press Convert (or Ctrl+Enter / Cmd+Enter). Auto-convert also fires 200 ms after each keystroke so the output updates as you type.
  6. Read the stats line: lines total, converted, errors, largest bit width, and the largest decimal decoded. Inspect the breakdown below to see each row's Gray → Binary → Decimal → Hex chain.
  7. Handle errors per line: a bad character shows a red row naming the position (e.g. "Line 3: invalid binary character '2' at position 2") while every other row still decodes.
  8. Copy or download. Copy uses the Clipboard API with an execCommand fallback for older browsers; Download saves a plain .txt.

Frequently Asked Questions

How is Gray code decoded into a number?

In two logical stages. Stage one: Gray → standard binary. The MSB passes through unchanged; every subsequent bit is the XOR of the previous output bit and the current Gray bit. Stage two: read that binary as an unsigned integer and format it however you want (hex, decimal, or keep it as binary). Gray 1011 → binary 1101 → hex 0xd (decimal 13).

Why do you make me choose between binary and hex input?

Because auto-detection is ambiguous. The string 1011 is a valid binary Gray code (4 bits, value 13 after decoding) AND a valid hex number (4113 decimal). Guessing based on length or characters would silently give wrong answers on ambiguous inputs. Picking explicitly means the tool always does what you expect.

What does “hex-packed Gray” mean?

It’s when you’ve compressed each nibble of Gray code bits into a hex digit for storage. 0xA represents the 4 Gray bits 1010; 0xDE represents the 8 bits 11011110. Set input mode to “Hex-packed Gray” and the tool unpacks the bits first, then runs the XOR decoder.

Does this handle Gray codes larger than 2^53?

Yes. The decoder uses JavaScript’s native BigInt, so a 256-bit or 1,024-bit Gray code decodes to its exact decimal and hex – no precision loss at 2^53 like you’d get with regular Number. The bit-parallel algorithm (binary ^= binary >> mask) scales linearly in the number of set bits.

Is my data kept private?

Yes. All processing happens in your browser’s JavaScript runtime – no bytes leave your machine. You can confirm this in the Network tab of DevTools: no requests fire during conversion. The tool also keeps working after you disconnect from the internet.

What happens if one line in my batch is invalid?

Only that line fails. The tool processes lines independently, so a stray character becomes one red row (“Line 3: invalid binary character ‘2’ at position 2”) while every other line still decodes. The stats line tells you exactly how many succeeded versus failed.

Does it work for rotary encoder readings?

Yes – that’s one of the main use cases. Absolute rotary encoders and shaft-position sensors emit Gray code because only one bit flips between adjacent positions, preventing transient glitches during transitions. Paste the raw encoder bits or its hex-packed form here to recover the integer position in hex.

Why hex output versus decimal?

Hex is four times more compact than binary (each hex digit is 4 bits) and aligns naturally with byte boundaries. It’s the standard format for logging microcontroller registers, debugging encoder firmware, and embedding values in C/Rust/JS source (0xAF style). If you want a human number instead, switch the output to “Decimal”.

What does the UPPERCASE toggle do?

It controls the case of alphabetic hex digits only. Uppercase on produces 0xAF, off produces 0xaf. The 0x prefix is always lowercase. The toggle is cosmetic – the underlying value is identical either way.

How do I go the other direction (hex back to Gray)?

Use a hex-to-Gray-code converter. The round trip is lossless: each Gray code maps to exactly one decoded value, and each decoded value re-encodes to exactly one Gray code. The encoder formula is gray = binary XOR (binary >> 1) – the inverse of what this tool applies.