Convert Hex to Gray Code

Convert hex to Gray code (reflected binary) with BigInt precision. Handles 32+ bit values correctly. Per-line errors, breakdown panel. Free, offline, client-side.

Encode a hexadecimal value as Gray code (reflected binary) using gray = bin ⊕ (bin >> 1) in BigInt arithmetic. Handles 32-bit, 64-bit, and arbitrarily large inputs correctly - plain JavaScript >> coerces to int32 and silently produces wrong answers past bit 31.

Enter hex to convert.

How to Use Convert Hex to Gray Code

  1. Paste your hex values, one per line. Leading 0x/0X prefix is optional; spaces and underscores (DE_AD_BE_EF) are stripped automatically. Case doesn't matter.
  2. Pick an output format: hex (default), binary (the decoded Gray bits), decimal (Gray as an integer), or all four columns tab-separated for spreadsheet paste.
  3. Toggle UPPERCASE hex for 0xAF style if your downstream system prefers it.
  4. Toggle the 0x prefix off if you need bare hex digits (e.g., for embedding in CSV or certain C macros).
  5. Press Convert (or Ctrl+Enter / Cmd+Enter). Auto-convert fires 200 ms after each keystroke so the output appears as you type.
  6. Read the stats line: total lines, successes, failures, the largest bit width seen, and the largest decoded Gray value.
  7. Inspect the breakdown: each row shows the full chain Hex-in → Binary-in → Gray-binary → Gray-hex, plus the Gray decimal for reference.

Frequently Asked Questions

How does hex-to-Gray-code encoding work?

The formula is gray = binary XOR (binary >> 1). In plain English: each Gray bit equals the XOR of the current binary bit with the bit immediately to its left. The highest bit carries through unchanged (XOR with 0). For hex 0xFF (binary 11111111), the Gray result is 10000000 (0x80) – only one bit set, because adjacent bits are all equal.

Why does this tool use BigInt instead of plain Number?

Because JavaScript’s ^ and >> operators silently coerce to signed 32-bit integers. For any value with bit 31 set (like 0xFFFFFFFF), bin >> 1 becomes an arithmetic shift of −1, which gives −1 back, so the XOR produces 0 instead of the correct 0x80000000. Using BigInt avoids this entirely – right shift on a non-negative BigInt is a logical shift at any width.

What’s the maximum hex value I can convert?

Practically unlimited. BigInt has no fixed upper bound, so 128-bit, 256-bit, even 1,024-bit hex values encode to Gray code without precision loss. A 64-bit input (0xFFFFFFFFFFFFFFFF) encodes to 0x8000000000000000, which regular Number arithmetic can’t represent at all.

Which hex input formats are accepted?

Bare hex digits (FF), C-style prefix (0xFF or 0XFF), space-separated (DE AD BE EF), underscore-separated (DE_AD_BE_EF), or mixed. Case-insensitive – aBc and ABC decode identically.

Why are Gray codes useful in real systems?

Because only one bit changes between consecutive values. In rotary encoders, this prevents transient glitches during transitions (binary would have multiple bits flipping at once, producing spurious intermediate readings). Gray codes also show up in Karnaugh map ordering, error-correcting codes, and certain clock-domain-crossing patterns.

What does the breakdown chain show?

For each successfully converted value: the hex input, its binary form (bit-width preserved), the Gray binary (after XOR >>1), and the Gray hex. Plus Gray decimal and bit count. It’s the fastest way to verify the encoding visually – you can see which bit positions flipped.

What happens if one line in my batch is invalid?

Only that line fails. The tool processes lines independently, so a stray character on one row becomes a red entry in the breakdown (“Line 3: invalid hex character ‘Z’ at position 2”) while every other line still converts. The stats row shows how many succeeded versus how many failed.

How do I reverse the conversion (Gray code back to hex)?

Use our “Gray code to hex” converter. The decoder uses a different formula: walk the Gray bits left to right, XOR-ing each with the running binary bit. Both directions are O(n) in bit length, and round-tripping any value loses no information.

Is my data uploaded anywhere?

No. All arithmetic runs in your browser’s JavaScript engine – no network requests fire during conversion, no cookies set, no analytics sent. Verify it yourself with your browser’s Network tab. The page works offline after the initial load.

What’s the difference between this and a binary-to-Gray-code tool?

Only the input format. This one accepts hex (more compact for humans); a binary-to-Gray-code tool accepts raw 0/1 bits (more explicit about bit width). Under the hood both apply the same gray = bin ⊕ (bin >> 1) transformation. Pick whichever matches your source data.