Methodology · Number Bases
Number base conversion methodology
Reviewed by Byron Malone · Last reviewed .
Every integer has exactly one representation in each positional base. The Dev Utilities Pro Number Base Converter converts integers between binary (base-2), octal (base-8), decimal (base-10), and hexadecimal (base-16), with optional two's complement representation for signed values at 8, 16, 32, and 64-bit widths. This page documents the algorithms, signed integer encoding, and Base64 encoding used in the calculator, with primary-source citations for each.
Base-N to decimal: Horner's method
Converting a number from base N to decimal uses Horner's method (Knuth, The Art of Computer Programming, Vol. 2, §4.4, Algorithm B), which evaluates the polynomial representation of the number in a single left-to-right pass with N multiplications:
// Input: digits[] = array of digit values left to right, base N // Example: "1A3" in base 16 → digits = [1, 10, 3] result = 0 for each digit d in digits (left to right): result = result * N + d // "1A3" hex: // result = 0 * 16 + 1 = 1 // result = 1 * 16 + 10 = 26 // result = 26 * 16 + 3 = 419
Horner's method is numerically stable and performs N multiplications and N additions for an N-digit number, the minimum possible for polynomial evaluation. The naive approach (compute baseiper digit) requires 2N−1 multiplications. For the digit ranges in this calculator (up to 64 binary digits, 16 hex digits), the difference is imperceptible in practice, but Horner's method is the canonical form per Knuth.
Valid digit characters per base: binary = [0, 1]; octal = [0–7]; decimal = [0–9]; hexadecimal = [0–9, A–F, a–f]. The calculator normalizes hex input to uppercase before conversion and rejects any character not in the valid digit set for the selected base.
Decimal to base-N: repeated division
Converting from decimal to base N uses the repeated-division algorithm (Knuth, TAOCP Vol. 2, §4.4, Algorithm A):
// Input: integer value V, target base N // Output: digits in base N, most-significant first digits = [] while V > 0: remainder = V mod N digits.prepend(remainder) // or collect and reverse at end V = floor(V / N) if digits is empty: digits = [0] // V = 0 case // Example: 419 in base 16 // 419 / 16 = 26 remainder 3 → digit '3' // 26 / 16 = 1 remainder 10 → digit 'A' // 1 / 16 = 0 remainder 1 → digit '1' // Result: "1A3"
The algorithm produces digits in least-significant-first order; they are reversed before display. For negative decimal inputs, the algorithm operates on the absolute value and then applies two's complement encoding if a bit-width is specified (see below).
Two's complement for signed integers
Two's complement is the universal method for representing signed integers in computer hardware. For an N-bit signed integer, the range is −2N−1 to 2N−1−1. The positive half occupies the lower half of the bit patterns (MSB = 0); the negative half occupies the upper half (MSB = 1). Specifically:
// Two's complement encoding of a negative value V in N bits // where -2^(N-1) <= V < 0 twos_complement = 2^N + V // Example: -1 in 8-bit two's complement // 2^8 + (-1) = 256 - 1 = 255 = 0b11111111 = 0xFF // Example: -128 in 8-bit two's complement // 2^8 + (-128) = 256 - 128 = 128 = 0b10000000 = 0x80 // Bit ranges per width: // 8-bit: -128 to 127 // 16-bit: -32,768 to 32,767 // 32-bit: -2,147,483,648 to 2,147,483,647 // 64-bit: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
The calculator displays two's complement in all four bases simultaneously. For 64-bit integers, JavaScript's native Number type (IEEE 754 double-precision float) loses integer precision at values beyond ±253(9,007,199,254,740,992). The calculator uses JavaScript's BigInt primitive for all 64-bit arithmetic to maintain exact integer representation.
Two's complement was chosen by hardware designers over sign-magnitude and one's complement because it has exactly one zero representation (not two, as in sign-magnitude where +0 and −0 differ), and because addition circuits require no special cases for sign: unsigned addition and two's complement addition use identical circuits. This property is documented in the x86-64 Intel Software Developer's Manual (Intel SDM, Vol. 1, §4.2.1) and is universal across modern ISAs.
Bit-width validation and overflow detection
The calculator validates that the entered decimal value fits within the selected bit-width. Validation logic:
// For a selected N-bit width: // Unsigned range check: if value < 0 OR value > 2^N - 1: error: "Value out of range for N-bit unsigned integer" // Signed range check: if value < -2^(N-1) OR value > 2^(N-1) - 1: error: "Value out of range for N-bit signed integer" // Example for 8-bit: // unsigned: 0 to 255 // signed: -128 to 127
When a value is in range for unsigned but not signed (e.g., 200 in 8-bit), the calculator displays both the unsigned interpretation and the signed two's complement interpretation (200 unsigned = −56 signed in 8-bit) alongside the bit pattern.
Base64 encoding (RFC 4648)
Base64 encodes arbitrary binary data as printable ASCII text. RFC 4648 defines the standard alphabet and encoding procedure. The encoding groups input bytes in sets of three and maps each group to four Base64 characters:
// RFC 4648 Base64 alphabet (Table 1): // Value 0–25: A–Z // Value 26–51: a–z // Value 52–61: 0–9 // Value 62: + (standard) or - (URL-safe variant, RFC 4648 §5) // Value 63: / (standard) or _ (URL-safe variant, RFC 4648 §5) // Encoding 3 bytes (24 bits) → 4 Base64 characters (24 bits): byte1 = 0bAAAAAA BB byte2 = 0bBBBB CCCC byte3 = 0bCC DDDDDD char1 = alphabet[AAAAAA] // bits 23–18 char2 = alphabet[BBBBBB] // bits 17–12 char3 = alphabet[CCCCCC] // bits 11–6 char4 = alphabet[DDDDDD] // bits 5–0 // Padding: // If input length mod 3 = 1: append two '=' padding characters // If input length mod 3 = 2: append one '=' padding character // If input length mod 3 = 0: no padding required
Base64 encoding increases data size by exactly 4/3 (33.33% overhead) before padding, and slightly more with padding for inputs whose length is not a multiple of 3. The URL-safe variant (RFC 4648 §5) replaces + with - and / with _to produce strings safe for use in URLs, filenames, and HTTP query parameters without percent-encoding. The calculator shows both standard and URL-safe variants simultaneously.
The calculator operates on UTF-8 encoded string input. Multi-byte UTF-8 characters (emoji, accented characters, CJK) are encoded as their UTF-8 byte sequence before Base64 encoding — the same behavior as btoa(unescape(encodeURIComponent(str)))in JavaScript or Python's base64.b64encode(str.encode('utf-8')).
ASCII table
The calculator includes an ASCII lookup table (U+0000 to U+007F) showing each character's decimal, hexadecimal, octal, and binary values. The ASCII standard (ANSI X3.4-1986, now INCITS 4-1986 R2012) assigns code points 0–31 to non-printing control characters, 32–126 to printable characters, and 127 to DEL. Unicode extends ASCII: the first 128 Unicode code points (U+0000 to U+007F) are identical to ASCII, and UTF-8 encodes them as single bytes with values 0x00–0x7F — making ASCII a strict subset of UTF-8.
Assumptions
- All integer arithmetic uses JavaScript
BigIntfor inputs exceeding ±253 to preserve exactness. - Base64 encoding assumes UTF-8 input encoding for string inputs; raw byte sequences are accepted as hex-encoded input.
- Two's complement display pads the binary output to the full N-bit width with leading zeros.
- Hexadecimal output uses uppercase letters (A–F) per the convention in Intel SDM and most hardware documentation.
Limitations
- Floating-point numbers are not supported — the converter operates on integers only. IEEE 754 floating-point bit layout visualization is a planned future feature.
- Base64 decoding of arbitrary binary (non-UTF-8) data displays the raw byte sequence in hex; it does not attempt to interpret the bytes as text.
- Bases other than 2, 8, 10, and 16 are not supported in this version.
Primary sources
- Knuth, Donald E. (1997). The Art of Computer Programming, Volume 2: Seminumerical Algorithms, 3rd ed., §4.4 “Radix Conversion.” Addison-Wesley. (Horner's method Algorithm B; repeated division Algorithm A)
- RFC 4648 — The Base16, Base32, and Base64 Data Encodings (IETF, 2006)
- Intel 64 and IA-32 Architectures Software Developer's Manual (Intel SDM), Vol. 1, §4.2 — “Fundamental Data Types” (two's complement integer representation)
- IEEE 754-2019 — Standard for Floating-Point Arithmetic (referenced for 64-bit double-precision integer precision limits: 253)
- ANSI X3.4-1986 / INCITS 4-1986 R2012 — American Standard Code for Information Interchange (ASCII)
Last reviewed by Byron Malone, 2026-05-23. This methodology document explains the algorithms used by the Dev Utilities Pro Number Base Converter. Verify outputs against your language runtime for edge cases involving 64-bit integer precision and platform-specific integer overflow behavior.
Back to methodology overview.