ForHosting KIT · Developer Utilities

Quaternary (base-4) converter

27 is 123 in base 4: each quaternary digit, from 0 to 3, holds exactly two bits, which is why base 4 suits DNA's four letters and two-bit codes. Convert whole numbers from decimal to quaternary and back. 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
Value
27
Direction
To quaternary
You get
Result
123
Decimal
27
Quaternary
123
Alphabet
0123

A quaternary converter rewrites a non-negative whole number between everyday base-10 and base-4, the four-digit positional system whose alphabet is simply zero, one, two, and three. Each base-4 digit packs exactly two bits, which makes quaternary a natural bridge for DNA-style four-letter alphabets, two-bit pixel channels, and compact state machines that only need four symbols. You choose a direction—decimal to quaternary or quaternary back to decimal—paste the value, and receive a deterministic result with the cleaned decimal integer, the base-4 digit string, the digit count, and the fixed alphabet. Any character outside zero through three fails with a clear invalid-input error instead of a silent wrong answer.

How quaternary (base-4) encoding and decoding work

Quaternary is positional notation with radix four. Each place is a power of four, just as decimal places are powers of ten: the units place is four to the zero, the next place is four, then sixteen, then sixty-four, two hundred fifty-six, and so on. The four digit symbols are zero, one, two, and three—nothing else.

That tiny alphabet is exactly what you need when a value lives in two bits: binary 00 maps to 0, 01 to 1, 10 to 2, and 11 to 3, so a long bit string can be regrouped into quaternary digits without padding games beyond an optional leading zero on an odd-length bit run.

To encode a non-negative decimal integer, divide repeatedly by four and record each remainder as the next least-significant digit; reverse those remainders to obtain the usual left-to-right string without leading zeros except for the number zero itself. To decode, walk the string from left to right, multiply the running total by four, and add the next digit—the classic Horner scheme. An optional 0q or 0Q prefix is stripped the same way binary tools accept 0b, so scripts can tag base-4 literals clearly.

An invalid character such as a space, plus sign, digit four or higher, letter, or punctuation mark aborts with invalid input naming the bad character and its position. Values must stay inside JavaScript’s safe integer range so every digit remains exact when the result is serialized. Negative inputs are rejected on purpose; the quaternary alphabet has no sign digit, and signed base-4 is a separate convention.

If you need arbitrary bases from two through thirty-six, that is a different search intent served by a general radix converter rather than this base-4-specific landing.

Where quaternary converters show up in real work

Bioinformatics tutorials and coding-theory courses introduce base four because DNA’s four nucleotides map cleanly onto two-bit codes and therefore onto quaternary digits: one common teaching map is A equals zero, C equals one, G equals two, and T equals three, so a short oligo becomes a compact base-4 string students can convert to decimal for hashing demos or puzzle keys.

Computer graphics and embedded firmware sometimes pack two-bit-per-channel data—palette indices, dither flags, or state machines with four modes—and engineers need a deterministic oracle that turns those digit strings into integers for tests without hand-rolling divmod in every language. Game designers invent four-state runes, inventory stacks, or alien numbering systems and need deterministic conversion for lore tools and save formats.

Treat general multi-base radix tables that stop at thirty-six, pure binary or hexadecimal dumps, and Base64 byte encoding as separate search intents with their own landings rather than stretching this integer quaternary converter past its honest scope.

DNA-style two-bit teaching maps

Map A/C/G/T drills onto digits 0–3, convert the base-4 string to decimal for hash demos, and check student answer keys with a shared oracle.

Classroom base-4 place-value drills

Convert homework integers between base-10 and base-4 so worksheets and auto-graders agree on every remainder and Horner step.

What alphabet does this quaternary converter use?

Only the four characters 0, 1, 2, and 3. There are no letter digits. An optional 0q or 0Q prefix is accepted on input and stripped before conversion.

What happens on an invalid quaternary digit?

The message names the bad character and its position. Only digits 0–3 are allowed after an optional 0q prefix; spaces, punctuation, letters, and digits 4–9 are rejected.

Is this the same as a general radix converter or binary?

No. General radix tools cover bases 2–36 with A–Z letter digits. Binary is base 2 with only 0 and 1. This page is dedicated to base-4 for DNA-style and two-bit packing search intent.

What does it cost?

The browser tool is free and runs locally.

How do I convert a decimal number to base 4 by hand?

Divide by 4 again and again and write down each remainder, which is always 0, 1, 2 or 3. Read the remainders from last to first: 27 gives 123, because 27 is one 16, two 4s and three units.

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/enc/base-quaternary

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/enc/base-quaternary \
  -H "Authorization: Bearer $KIT_KEY" \
  -H "Content-Type: application/json" \
  -d '{"value":"27","direction":"to_quaternary"}'
{
  "value": "27",
  "direction": "to_quaternary"
}
{
  "task_id": "tsk_a1b2c3d4e5f6a1b2c3d4e5f6",
  "type": "enc.base_quaternary",
  "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.

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.

Pricing for successful API calls is the published base of $0.002 per item.

How quaternary (base-4) encoding and decoding work

The algorithm uses exact integer arithmetic only: no floating-point rounding, no random seeds, no wall-clock timestamps, and no network calls, so identical inputs always produce identical JSON.

What you send and what you get back

Send two required fields: value and direction. Absolute decimal values larger than Number.MAX_SAFE_INTEGER are rejected so low-order digits cannot silently disappear. Classic checks include twenty-seven encoding to one-two-three in base four, sixteen encoding to one-zero-zero, and decoding three-two-one back to fifty-seven. Direction must be to_quaternary when value is a non-negative decimal integer, or from_quaternary when value is a base-4 string. Aliases are accepted so forms and scripts can use the names that fit their UI: value also accepts n, number, input, text, decimal, quaternary, and base4; direction also accepts dir, mode, and op, plus short synonyms such as to, from, encode, decode, to_base4, from_base4, decimal_to_quaternary, and quaternary_to_decimal. For to_quaternary, value may be a JSON integer or a digit string with an optional leading plus; fractions, scientific notation, bare words, NaN, Infinity, and negatives fail with invalid_input and are not charged. For from_quaternary, every character must be zero, one, two, or three after an optional 0q prefix; any other character—including four, five, underscore, hyphen, and space—fails with a message that names the invalid quaternary digit and its zero-based position. On success the response echoes the cleaned value string, the normalized direction, decimal as a number, quaternary as the compact base-4 string, result as the primary converted form (quaternary when encoding, decimal string when decoding), the alphabet string 0123, base set to four, and digit_count for the quaternary representation. Pricing for successful API calls is the published base of $0.002 per item; validation failures do not consume prepaid balance.

Where quaternary converters show up in real work

Because this capability is a pure parse engine, API pricing is the published base of $0.002 per successful item and failed validation does not consume balance.

Two-bit pixel and state packing

Encode palette indices or four-mode flags as quaternary digit strings and decode them back to integers in unit tests and firmware fixtures.

How do I choose the direction?

Use to_quaternary when value is a non-negative decimal integer and you want a base-4 string. Use from_quaternary when value is already quaternary and you want the decimal integer back.

Read the full KIT documentation →