Skip to content

Gray Code to Binary Converter

Convert Gray code (reflected binary code) to standard binary or decimal for decoding rotary encoders, position sensors, and error-corrected data

Category: data
Use Case: Encoder Decoding, Position Reading, Data Recovery
Privacy: 100% browser-based

Conversion Options

0 = no padding

Gray to Binary conversion:
First bit stays same, then XOR each Gray bit with previous binary bit.
Example: Gray 110 → Binary: 1 (same), 1⊕1=0, 0⊕0=0 → 100

Recommended Settings

Pro Tips

  • Gray code conversion: first bit stays same, then XOR each subsequent Gray bit with previous binary bit
  • Reverse of binary to Gray: instead of XOR with shifted version, XOR progressively
  • Used to decode position sensors and rotary encoders that output Gray code
  • Single-bit changes in Gray code ensure no intermediate errors during decoding

Most Popular

Most users convert to binary for further processing or decimal for direct position values

When to Use This Tool

Rotary Encoder Decoding

Decode position data from rotary encoders and shaft encoders that output Gray code signals. Convert Gray code readings to binary or decimal position values for motor control, robotics, and CNC machines. Essential for accurate position tracking without transition errors.

Position Sensor Data Processing

Process absolute position sensors and linear encoders that transmit Gray code for error-free position reading. Convert Gray code to standard binary for microcontroller processing, PLC systems, and industrial automation. Critical for precision measurement systems.

Error-Corrected Data Recovery

Recover data from communication systems and storage devices that use Gray code for error correction. Convert received Gray code back to binary for data processing. Common in digital communication protocols and error-detection systems where single-bit transitions are monitored.

Digital Logic Design & Testing

Test and verify Gray code counters, state machines, and Karnaugh map implementations in digital circuits. Convert Gray code outputs to binary for analysis and debugging. Essential for FPGA development and digital system verification where Gray code minimizes glitches.

How It Works

1

Parse input Gray code strings (binary strings containing only 0s and 1s)

2

Remove whitespace and optional 0b prefix from Gray code input

3

Validate that input contains only binary digits (0 and 1)

4

Apply conversion algorithm: first bit of binary equals first bit of Gray code

5

For each subsequent bit: XOR current Gray bit with previous binary bit

6

Build binary string progressively by applying XOR operation left-to-right

7

Convert to decimal if requested, or format binary with padding/grouping

100% Private

Files never leave your device. All processing happens locally in your browser.

Lightning Fast

Powered by Pure JavaScript for optimal performance on modern browsers.

Secure

No data collection, no tracking, no sign-up required.

Frequently Asked Questions

How does Gray code to binary conversion work?

The algorithm works progressively: (1) First binary bit = first Gray bit (unchanged). (2) For each remaining position, XOR the current Gray code bit with the previous binary bit. For example, Gray code 110 converts to binary: 1st bit=1 (same), 2nd bit=1⊕1=0, 3rd bit=0⊕0=0, result=100 (decimal 4).

Why would I need to convert Gray code back to binary?

Rotary encoders, position sensors, and absolute encoders output Gray code to eliminate reading errors during transitions. To use this position data in software or digital systems, you need to convert it back to standard binary or decimal. This conversion recovers the actual position value from the error-free Gray code representation.

Is Gray to binary conversion the exact reverse of binary to Gray?

Conceptually yes, but algorithmically different. Binary to Gray uses: gray = binary XOR (binary >> 1). Gray to binary uses progressive XOR: each binary bit is the XOR of all Gray code bits up to that position. Both are reversible - converting binary→Gray→binary returns the original value.

Can I convert Gray code directly to decimal?

Yes, this tool offers direct Gray code to decimal conversion. It first converts Gray to binary (using the XOR algorithm), then converts the binary to decimal. This is useful for position sensors where you need the numeric position value directly without intermediate binary representation.

What happens if my Gray code has leading zeros?

Leading zeros are preserved during conversion. Gray code 0110 converts to binary 0100 (same as decimal 4). If you enable padding in binary output mode, the result will maintain the specified bit width. This is important for fixed-width encoder systems where position values must have consistent bit lengths.

Why do encoders use Gray code instead of binary?

Gray code ensures only one bit changes between consecutive values, eliminating ambiguous readings during transitions. In binary, 3→4 changes three bits (011→100), potentially reading garbage values mid-transition. Gray code 3→4 changes only one bit (010→110), so you read either old or new value, never invalid data.