NUMBER REPRESENTATION

Checking why integer conversion removes leading zeros

An integer base converter keeps the value of a number and drops its leading zeros. This worksheet converts 000123 to decimal and to hexadecimal, then explains when that behavior is the wrong tool for the data.

What normalization means here

A number is a quantity, and a numeral is one way of writing it. The numerals 123, 0123 and 000123 all name the same quantity in base 10, so a converter that works on quantities has no reason to keep the zeros in front. The integer base converter parses the digit string with arbitrary-precision integer arithmetic, then prints the result using the digits of the target base, and the printed form never starts with a redundant zero. Entering 000123 in base 10 and asking for base 10 therefore returns 123. The tool is doing what it says: converting the number. If the zeros carried meaning of their own, such as a fixed width, then the field was never really a number, and the converter is the wrong operation to apply to it. A useful mental picture is a ledger. The converter reads the amount in the ledger and rewrites it in a different currency of digits. It does not copy the handwriting of the original entry, so a flourish such as a run of zeros in front does not survive the rewrite. The same reasoning applies to a decimal point: this tool handles integers only, so a fractional part is out of scope, and input such as 1.5 is rejected.

The decimal and hexadecimal results

With the source and target both set to base 10, the input 000123 returns 123. With the target set to base 16, it returns 7B. The hexadecimal result comes from repeated division: 123 divided by 16 is 7 with a remainder of 11, and 11 is written B in hexadecimal, so the digits are 7 followed by B. Both outputs represent the same integer. Neither keeps the six character width of the source. A reader who wants to confirm the hexadecimal digits can multiply back: 7 times 16 is 112, and adding 11 gives 123. The output uses uppercase letters for digits above 9, so B appears in capitals, which is worth remembering when comparing against lowercase values from other tools. As another small check, 255 in decimal is FF in hexadecimal, and 256 is 100. The step from FF to 100 shows how a new leading digit appears only when the value requires it.

Other bases for the same value

The same integer, 123, takes different spellings in other bases. In base 2 it is 1111011, since 64 plus 32 plus 16 plus 8 plus 2 plus 1 equals 123. In base 8 it is 173, because 1 times 64 plus 7 times 8 plus 3 equals 123. In base 36, which uses the digits 0 to 9 followed by the letters A to Z, it is 3F, as 3 times 36 plus 15 equals 123. The converter accepts bases from 2 to 36, so every one of these can be produced from the same input. Each result is shorter or longer than the decimal form depending on the size of the base, and none carries the leading zeros of the source. Fewer symbols are needed when the base is larger. Base 36 is used in short identifiers because it packs more information per character, and 36 to the power 6 exceeds two billion.

When the zeros are part of the data

Leading zeros matter whenever a field is an identifier and not a quantity. Many postal codes in the United States begin with a zero, so the code for some New England addresses loses a digit if a spreadsheet treats it as a number. Account numbers, employee badges, product codes and telephone prefixes behave the same way. These fields are labels that happen to use digits. Arithmetic on them has no meaning: adding two ZIP codes produces nothing useful. The test is simple. If you would never add, subtract or compare the values by size, store them as text. Treating them as numbers silently drops the zeros, and the original width cannot be recovered from the number alone. A fixed-width record format, such as an old banking file with a ten digit account field, relies on zero padding to keep columns aligned. Read those fields as text, and pad again on output.

Zeros that carry width in binary and hexadecimal

Leading zeros also appear in positional notation for fixed-width data. A byte written in binary as 00001111 has eight bits, and the four leading zeros document the width. As a number, it is 15. Converted to hexadecimal by this tool, it appears as F and not 0F, even though two hex digits are customary for a full byte. If a downstream system expects exactly two characters, pad the result to the required length after conversion. The converter does not know how wide the original field was, and it cannot invent a width that the input string does not state. Padding is a formatting step and belongs in a separate, explicit stage of the workflow.

Octal prefixes in programming languages

A leading zero has a special meaning in some programming languages. In C and several languages derived from it, a literal such as 0123 is read as octal, so its value is 83 and not 123, because 1 times 64 plus 2 times 8 plus 3 equals 83. Older JavaScript treated numeric literals with a leading zero the same way, and modern strict mode forbids them. This quirk is a reason to keep the base explicit. The converter asks for the source base separately, so 000123 with a source base of 10 is 123 and with a source base of 8 is 83. Stating the base removes the guesswork that language rules would otherwise introduce. Modern code usually writes octal with an explicit prefix such as 0o, which avoids this trap entirely.

Converting back from hexadecimal

The reverse direction shows the same normalization. Entering 007B with a source base of 16 and a target base of 10 returns 123. The two leading zeros disappear because they add nothing to the value. The conversion is lossless for the number and lossy for the text spelling. A round trip from a padded string to a number and back to text therefore does not reproduce the original. If your process needs the original back, keep it. The converted value is a second view of the data and does not replace the source representation. Padding functions in most languages restore a width on demand, for example by filling a string with zeros on the left up to a stated length.

Signed values

The converter parses signed digit strings, so a leading minus sign applies to the whole number. The input -000123 in base 10 converts to -123, and to -7B in base 16. The minus sign belongs to the number as a whole and is not a digit, so it stays in front after conversion. Zeros after the sign are still redundant and are removed. The tool does not interpret two's complement or any fixed width representation, which means negative values in a register dump written in hexadecimal need a separate step to read correctly. A string such as FFFFFF85 means 4,294,967,173 here, not negative 123. If a fixed width matters, convert the magnitude first and apply the width rule yourself.

Large values and exactness

Because the parsing uses arbitrary-precision integers, long digit strings are converted exactly. Ordinary floating-point numbers in many languages hold integers exactly only up to 9,007,199,254,740,991, about 9 quadrillion, and larger values lose low digits. A 20 digit identifier pasted into a tool that uses such numbers would be silently changed. The converter avoids that for integer base conversion, so the digits you get back correspond exactly to the number you supplied, up to its limit of 4,096 digits. Exactness of the value still says nothing about the width of the text, so very long identifiers with leading zeros need the same text-preserving care as short ones. A product barcode number is a good example of a long identifier: it should travel as text from start to finish, and any base conversion should be applied to a copy used only for analysis.

A decision rule for the workflow

A short rule decides whether conversion is appropriate. If the field has units, can be compared by size, or is the result of arithmetic, convert it as a number and accept normalized output. If the field is a label, a code or a fixed-width record, keep it as text and apply base conversion only to a copy used for a specific purpose, such as inspecting its numeric value. Write down the width when it matters, and apply padding as a final formatting step. The CSV and JSON guides on this site show how to keep such fields as text through those formats.

What the converter does not do

The tool does not guess the schema of an identifier, restore a lost width, or decide that a string of digits is a code and not a number. It reads the digit string you give it in the base you state and returns the same quantity in the base you request. Invalid digits for the chosen base, such as the digit 9 in base 8, are rejected. The worksheet results of 123 and 7B are exact for the input 000123. Anything beyond the numeric value belongs to the person who knows what the field means. Input is case-insensitive, so 7b and 7B both read as 123, and an embedded space is an error, not a separator.

Checking a conversion by hand

Two short procedures let you verify any result without the tool. To convert a base 10 number to another base, divide repeatedly by the target base and read the remainders from last to first. For 123 and base 16, the first division gives 7 remainder 11, and 7 divided by 16 gives 0 remainder 7, so the digits read 7 and then B. To go the other way, multiply and add: starting from 0, take each digit in turn, multiply the running value by the source base and add the digit. For 7B in base 16 that is 0 times 16 plus 7, which is 7, then 7 times 16 plus 11, which is 123. These steps show why a leading zero never matters. Multiplying zero by any base and adding a zero digit leaves the running value at zero.

Reproduce the checks

Decimal normalization

Open the Integer Base Converter and enter:

Value
000123
Source base
10
Target base
10

Expected output:

Value

123

Numeric conversion removes redundant leading zeros.

Hexadecimal representation

Open the Integer Base Converter and enter:

Value
000123
Source base
10
Target base
16

Expected output:

Value

7B

The value is preserved while the digit representation changes.

References and implementation scope

The examples describe the behavior of the linked Computandum tools. A successful local check does not establish that another application accepts the same format or applies the same limits.

Sources

Related tasks