ForHosting KIT · Engineering & construction

Gray code to binary calculator for encoder values

Gray 0111 decodes to binary 0101, which is 5 in decimal. Paste the word your encoder reports, with or without a 0b prefix, and get ordinary binary with the same width and every leading zero kept. Free in your browser.

  • Free
  • Runs in your browser
  • No sign-up
  • Your data stays on your device
Use it from WebAPIEmailTelegramApp soon
If you enter
Gray code
0111
You get
Binary
0101
Decimal
5
Bit width
4

A reflected Gray code changes only one bit between adjacent positions, which helps rotary and linear encoders avoid ambiguous readings near a transition. The value is useful at the sensor boundary, but most software ultimately needs ordinary binary. This calculator reverses the Gray encoding with an exact cumulative XOR chain from the most significant bit to the least significant bit. It accepts a clean bit string, preserves leading zeros and width, and returns binary plus its decimal value.

Understand the cumulative XOR that reverses reflected Gray code

Reflected Gray code is arranged so neighboring numeric positions differ in exactly one bit. That single-change property reduces the chance that an encoder is interpreted as a wildly different position while several ordinary binary bits would otherwise switch at once. Decoding starts at the most significant side.

The first binary bit is copied directly from the first Gray bit. Every later binary bit equals the previous binary bit XOR the current Gray bit. An equivalent description says that each binary position is the XOR, or parity, of every Gray bit from the left edge through that position.

For Gray 0111, the running parity is 0, then 1, then 0, then 1, producing binary 0101. This capability applies that chain character by character instead of relying on JavaScript bitwise integers, which are limited to 32-bit signed operations. As a result, fixed-width encoder words and long teaching examples remain exact.

Decode an encoder word and read the result

An optional 0b prefix is accepted when you are copying a literal from firmware documentation. Leading zeros are significant for presentation and are preserved: a 12-bit sensor word remains 12 bits even when its numeric magnitude is small. Strict validation is intentional because silently deleting a character could turn a hardware position into a plausible but incorrect reading.

If a source formats bits with separators, remove those separators deliberately before calling this capability rather than depending on an ambiguous cleanup rule.

Use the result in encoder diagnostics, firmware tests, and data pipelines

For a quick bench check, paste the Gray word printed by an encoder datasheet or captured from a register and compare binary with the position your mechanism should report. The computation is deterministic and performs no network access, random generation, clock reading, or stateful caching, so the same input always produces the same output. Remember that conversion alone does not validate wiring order, detect a damaged encoder, apply wraparound, account for mechanical gearing, or translate a decoded count into degrees or millimeters.

Those steps belong to the device-specific layer. Confirm whether the manufacturer transmits the most significant bit first and whether it uses ordinary binary-reflected Gray code before treating the decimal field as a physical position.

Rotary encoder bench checks

Decode a captured Gray word into ordinary binary and decimal while preserving the encoder's declared bit width for comparison with its expected shaft position.

Sensor data normalization

Convert archived reflected Gray fields before calibration or analysis while retaining the normalized source code and conversion method in structured output.

How is reflected Gray code converted to binary?

Copy the most significant Gray bit, then XOR each following Gray bit with the previous binary bit. This is the same as taking a running XOR from left to right.

Can I include a 0b prefix?

Yes. A leading 0b or 0B is removed before conversion. Other punctuation, separators, and whitespace inside the word are rejected.

Does this support every kind of Gray code?

No. It decodes standard binary-reflected Gray code. Custom manufacturer mappings, balanced Gray codes, and non-binary variants are outside scope.

What does the calculator cost?

The browser calculator runs free on your device.

Can I paste a Gray code that contains spaces or separators?

No. The word may hold only zeros and ones, plus an optional 0b prefix. Remove spaces or separators yourself first: silently dropping a character could turn a real encoder position into a plausible but wrong reading.

Everything on this page is available programmatically. This section is for teams who want to wire it into their own systems; everyone else can just use the tool above.

POSThttps://api.kit.forhosting.com/elec/gray-to-binary

Prefer to automate it? One authenticated POST creates the task; the result comes back by webhook or a signed link. The same capability also runs here on the web, by email and from Telegram — and soon from our app too.

curl -X POST https://api.kit.forhosting.com/elec/gray-to-binary \
  -H "Authorization: Bearer $KIT_KEY" \
  -H "Content-Type: application/json" \
  -d '{"gray_code":"0111"}'
{
  "gray_code": "0111"
}
{
  "task_id": "tsk_a1b2c3d4e5f6a1b2c3d4e5f6",
  "type": "elec.gray_to_binary",
  "status": "queued",
  "_links": {
    "result": "/tasks/tsk_…/result"
  }
}

The API is asynchronous: the call returns a task_id immediately and the result arrives by webhook. Polling is capped at 1 req/s per task.

Per request$0.002

Published price — no tokens, no invented credits. A failed task is never charged.

max_bits4096
HTTPCodeMeaning
401unauthorizedMissing or invalid API key.
402insufficient_balanceYour balance doesn't cover the task price.
404unknown_typeThat task type doesn't exist.
429rate_limitedToo many requests. Use the webhook instead of polling.

The browser calculation is free, while the same deterministic API contract is available for $0.002 per successful item.

Understand the cumulative XOR that reverses reflected Gray code

The returned method field names cumulative_xor explicitly so automated consumers can record which Gray convention was decoded. This calculator implements standard binary-reflected Gray code; unrelated balanced, n-ary, or custom manufacturer codes require their own mapping tables and are outside its contract.

Enter an encoder word and interpret every response field

Provide gray_code as a most-significant-bit-first string containing only zero and one. The aliases gray, value, and bits are also recognized, which makes the endpoint easier to connect to existing forms or telemetry transformations, but gray_code is the clearest canonical name. The response repeats the normalized gray_code, places the decoded standard representation in binary, reports decimal as a safe JSON number when possible, and uses a decimal string when the result would exceed JavaScript's exact integer range. bit_width reports the number of actual code bits, excluding an optional prefix, while method is always cumulative_xor. Empty input, embedded spaces, signs, decimal digits other than zero or one, and strings longer than the published 4096-bit limit fail with invalid_input.

Use the result in encoder diagnostics, firmware tests, and data pipelines

Data pipelines can decode archived sensor words before joining them to calibration tables, while preserving gray_code and bit_width as traceable source metadata. Each successful API item costs $0.002; interactive browser use runs locally for free.

Firmware regression fixtures

Generate stable reference results for tests of embedded cumulative XOR routines without depending on 32-bit host-language bitwise behavior.

Are leading zeros preserved?

Yes. Both gray_code and binary retain the original bit width, and bit_width reports it explicitly.

Read the full KIT documentation →