
Binary, hexadecimal, and decimal: choose the right representation
A number’s base changes its notation, not its mathematical value, but software systems often attach width and sign rules to the representation.
Decimal 2026, binary 11111101010, and hexadecimal 0x7EA all name the same quantity. The digits look very different, yet each one is just a way of writing that quantity with a different set of symbols and place values. If you work with colour codes, file formats, network packets, permissions, flags, memory dumps, or low-level debugging, you will meet all three bases regularly, and the skill that matters is not memorising conversions. It is knowing which representation to read, which one to write, and what extra information – width, signedness, byte order – a bare number does not tell you.
This guide explains how binary, hexadecimal, and decimal work, how to convert between them by hand and with a tool, how to choose the right representation for a task, and where conversions can mislead you. It ends with worked examples, limitations, and answers to common questions.
What a base actually means
A base, also called a radix, is the number of distinct digits a system uses, and the multiplier applied at each place as you move left. Decimal, base 10, uses the digits 0 through 9. Binary, base 2, uses on
Decimal: the base for human quantities
Decimal is the base most readers use for counting, money, dates, measurements, and ordinary quantities. Its advantage is familiarity. If you tell someone a file is 2,026 bytes, a timeout is 30 seconds, or a price is 49.99, nobody has to decode the notation first. That is why decimal is usually the right choice when the audience is a person rather than a machine, and when the exact bit pattern does not matter.
Decimal is a poor choice when the bit pattern does matter. Looking at 255 does not immediately show that every bit in a byte is set, while 11111111 or 0xFF makes that obvious. Decimal also hides alignment. Two decimal numbers that look unrelated, such as 240 and 15, are complementary bit masks in binary and hex: 0xF0 and 0x0F. Use decimal when you are communicating a quantity. Use another base when you are communicating a pattern.
Binary: the base for individual bits
Binary is the base that matches how digital hardware stores information. A bit is a single binary digit, 0 or 1. A byte is normally eight bits. A nibble is four bits, which is exactly one hexadecimal digit. A word is a larger fixed-size group, commonly 16, 32, or 64 bits, depending on the system.
Binary is verbose, which is both its strength and its weakness. It shows every bit, so it is the clearest base for inspecting flags, masks, permissions, and single-bit fields. For example, the permission bits, the flags in a protocol header, or the individual options packed into a configuration byte are easiest to reason about in binary. Binary becomes hard to read once numbers grow. A 32-bit address written as 32 ones and zeros is easy to mistype and hard to scan, which is why l
How to convert between bases
Binary to decimal
Write the place values above the bits, starting with 1 on the right and doubling as you move left: 1, 2, 4, 8, 16, 32, 64, 128, and so on. Add the place values where the bit is 1 and ignore the places where the bit is 0. For 0b101101, the set bits are 32, 8, 4, and 1, which sum to 45. For 0b11111111, the sum is 128 + 64 + 32 + 16 + 8 + 4 + 2 + 1, which is 255.
Decimal to binary
Use repeated division by two. Divide the number by two, write down the remainder, which will be 0 or 1, and repeat with the quotient until the quotient reaches zero. Then read the remainders from last to first. For 45: 45 / 2 = 22 remainder 1; 22 / 2 = 11 remainder 0; 11 / 2 = 5 remainder 1; 5 / 2 = 2 remainder 1; 2 / 2 = 1 remainder 0; 1 / 2 = 0 remainder 1. Reading the remainders upward gives 101101, which matches the result above.
A quicker method for small numbers is to subtract the largest power of two that fits, write a 1 for that place, and repeat with the remainder. For 45, subtract 32 to leave 13, subtract 8 to leave 5, subtract 4 to leave 1, subtract 1 to leave 0. The places used are 32, 8, 4, and 1, giving 101101 again. Use whi
Endianness: the order of the bytes
Endianness describes how the bytes of a multi-byte value are arranged in memory or in a file. It is separate from the base. The value 0x1234 has two bytes, 0x12 and 0x34. In big-endian order, the most significant byte comes first: 12 34. In little-endian order, the least significant byte comes first: 34 12. The numeric value is the same either way, but the byte sequence is reversed.
That distinction matters when you read a hex dump, parse a binary file, or send a number over a network. If you assume the wrong byte order, you will read 0x1234 as 0x3412, which is a completely different number. Network protocols commonly specify big-endian, often called network byte order, while many common processors store integers little-endian. File formats state their byte order in the specification, and some include a magic number or byte-order mark so a reader can detect it. When a hex dump looks plausible but the values are wrong, checking the byte order is one of the first things to try.
Choosing the right representation
<
| Notation | Example (value 2026) | Base | Typical use |
|---|---|---|---|
| Decimal | 2026 | 10 | Human quantities |
| Binary | 11111101010 | 2 | Individual bits |
| Hexadecimal | 0x7EA | 16 | Compact way to read bits |
p>Use hexadecimal when grouping bits into readable nibbles, binary when inspecting individual flags, and decimal when communicating quantities to most readers. That rule covers most cases. A few more guidelines help with the rest.
- Use hex for addresses, offsets, dumps, hashes, masks, and colour values, where the bit pattern matters but binary would be too long.
- Use binary for flag fields, bit masks, permission bits, and teaching, where each individual bit is the point.
- Use decimal for counts, sizes in a user-facing message, durations, prices, and anything a non-technical reader will act on.
- Use the representation the surrounding system uses. If a specification writes lengths in decimal and offsets in hex, follow that split rather than normalising everything to one base.
- When you change bases, keep the width and signedness with the value. Write 0xFF as uint8, or 0b11111111 as eight bits, rather than leaving the reader to guess.
Consistency also matters inside a single document or codebase. Mixing 0xff, 0XFF, FFh, and 255 for the same value in different places makes searching harder and invites copy-and-paste errors. Pick one style for each context, document it, and keep the prefix or suffix that identifies the base.
Common mistakes to avoid
- Reading a hex number as decimal. The string 10 in hex is 16 in decimal, and the string 19 in hex is 25 in decimal. The digits look familiar, but the place values are different.
- Forgetting that A through F are digits. In hex, F is a single digit with the value 15, not a letter or a separate number.
- Dropping leading zeros that carry width information. 0x00FF and 0xFF have the same value but different stated widths, and in a fixed-width protocol that difference can matter.
- Confusing byte order with base. Converting 0x1234 to binary does not change its byte order. Reversing the bytes is a separate step.
- Assuming a bare hex value implies signedness or width. A bare 0xFF could be an unsigned byte, a signed byte equal to -1, part of a wider integer, or one byte of a longer sequence.
- Trusting a hand conversion without a round-trip check. Convert back by a different route, or verify with a tool, before the value goes into a specification, a test, or a configuration.
Limitations
Base conversion is exact for integers, but it does not solve every numeric problem. Fractional values, such as 0.1 in decimal, may not have an exact finite representation in binary, which is why floating-point arithmetic has rounding behaviour that surprises people who expect decimal arithmetic. Very large integers may exceed the fixed width of a type in one language while remaining exact in another, and converting between them can lose precision if a floating-point intermediate is used. Dates, times, and measurements add further rules – units, time zones, calendars, and precision – that a base conversion does not capture. A converter is a notation aid, not a substitute for checking the type, width, signedness, byte order, and range that the receiving system expects.
Use the related tool carefully
This guide is paired with a browser-only utility. The tool is a practical aid for small examples and inspection; it is not a substitute for application testing, a formal data pipeline, or professional review where the decision has higher consequences.
Open the related Number Base Converter
Frequently asked questions
Why is hexadecimal used instead of just writing binary?
Hexadecimal is a compact shorthand for binary. Each hex digit stands for exactly four bits, so a 32-bit value needs eight hex digits instead of 32 binary digits. It keeps the bit pattern visible while staying short enough to read, copy, and compare. Binary is clearer when you need to see each bit, but for longer values hex is usually the better balance.
How do I know whether a number is binary, decimal, or hexadecimal?
Look for a prefix, a suffix, or a statement in the surrounding context. 0b means binary, 0x means hexadecimal, and a bare number is usually decimal unless the context says otherwise. If there is no marker, check the digits: any occurrence of A through F means the value is not decimal, and a long string of only 0s and 1s may be binary. When the context gives no clue, do not guess. Ask for the base, width, and signedness to be stated explicitly.
Does converting a number change its value?
No, for integers. Conversion changes only the notation. Decimal 45, binary 101101, and hexadecimal 0x2D all name the same quantity. What can change the value is a different width, signedness, byte order, or rounding rule applied during storage or transmission. If a conversion appears to change the value, check those rules rather than the arithmetic of the base change itself.
What does 0xFF mean, and is it 255 or -1?
It depends on the type. As an unsigned eight-bit integer, 0xFF is 255. As a signed eight-bit integer in two’s complement, the same bit pattern is -1. The hex notation alone does not answer the question. You need the width and signedness from the specification, type, or protocol. When you document a value, state those rules alongside it so the next reader does not have to guess.
Sources
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt
- https://tc39.es/ecma262/
Worked example: convert 2026 end to end
Start with decimal 2026. To convert to binary by place values, list the powers of two up to 1024: 1024, 512, 256, 128, 64, 32, 16, 8, 4, 2, 1. Walk from the largest to the smallest, keeping a running remainder. 2026 minus 1024 leaves 1002. Minus 512 leaves 490. Minus 256 leaves 234. Minus 128 leaves 106. Minus 64 leaves 42. Minus 32 leaves 10. The next power, 16, does not fit, so that bit is 0. Minus 8 leaves 2. The next power, 4, does not fit, so that bit is 0. Minus 2 leaves 0. The final power, 1, does not fit, so that bit is 0. The bits, in order, are 11111101010.
To convert that binary value to hex, pad it to a multiple of four bits: 0111 1110 1010. The groups convert to 7, E, and A, so the hex form is 0x7EA. To check the result, expand back: 7 x 256 + 14 x 16 + 10 = 2026. The round trip closes, which is the check that matters. Whenever you convert by hand, convert back by a different route. If the two routes disagree, the error is in one of the conversions, not in the number.
Now try a smaller example that shows why grouping matters. Decimal 45 is 0b101101 and 0x2D. In an eight-bit field, the same value is written as 0b00101101 and 0x2D, or as 0x2D with an explicit note that the field is one byte. If you saw 0b101101 in a protocol description without a stated width, you would not know whether the field was six bits, eight bits, or part of a longer field. That missing width information is a common source of interoperability bugs, and it is why specifications usually state the width alongside the value.
Width, signedness, and overflow
A base tells you how a number is written. It does not tell you how many bits the system uses to store it, whether it can be negative, or what happens when a calculation exceeds the range. Those rules come from the type, format, or protocol, and they change the meaning of the same digits.
An unsigned eight-bit integer holds 0 to 255. A signed eight-bit integer using two’s complement holds -128 to 127. In both cases, the bit pattern 11111111 is a valid stored value, but the unsigned reading is 255 and the signed reading is -1. In two’s complement, negative numbers are formed by inverting the bits and adding one. So -1 in eight bits is 00000001 inverted to 11111110, plus one, giving 11111111. If you interpret a signed byte as unsigned, or an unsigned byte as signed, values above 127 will be wrong.
Overflow is the related failure. Adding 1 to an unsigned eight-bit 255 gives 256, which needs nine bits. In a fixed-width system that result may wrap to 0, saturate at 255, raise an error, or be truncated, depending on the language and context. In a language with arbitrary-precision integers, such as Python integers or JavaScript BigInt, the value simply grows. Do not assume wrapping, saturation, or an error. Check the documented behaviour for the type and operation you are using, and test the boundary values: the minimum, the maximum, one below the minimum, and one above the maximum.
chever method you check more reliably. For long values, a converter is safer than either method, because one dropped digit changes the result.
Hexadecimal to decimal
Use place values that are powers of 16: 1, 16, 256, 4096, and so on. Convert each hex digit to its decimal value, multiply by its place value, and add. For 0x2D, the digits are 2 and 13, so the value is 2 x 16 + 13 x 1 = 45. For 0x7EA, the digits are 7, 14, and 10, so the value is 7 x 256 + 14 x 16 + 10 x 1 = 1792 + 224 + 10 = 2026.
Decimal to hexadecimal
Use repeated division by 16, the same way you used division by two for binary. Divide by 16, record the remainder as a hex digit, and repeat with the quotient until it reaches zero. Read the remainders from last to first. For 2026: 2026 / 16 = 126 remainder 10, which is A; 126 / 16 = 7 remainder 14, which is E; 7 / 16 = 0 remainder 7. Reading upward gives 0x7EA. The same method works for any base: divide by the base, record remainders, read them back in reverse.
Binary to hexadecimal, and back
This is the easiest conversion because four bits map to one hex digit. To go from binary to hex, group the bits in fours from the right, pad the leftmost group with leading zeros if it is short, and convert each group. For 101101, group it as 0010 1101, which is 0x2D. To go from hex to binary, expand each hex digit to four bits. For 0x7EA, expand 7 to 0111, E to 1110, and A to 1010, giving 0111 1110 1010. Dropping the leading zero gives 11111101010, the binary form of 2026.
onger binary values are usually grouped, spaced, or converted to hex for display.
When you write binary by hand, group digits in fours from the right and use separators if the context allows them. Writing 1111 1110 1010 is far easier to check than 11111101010. Keep leading zeros when width matters. The value 0b00001010 and the value 0b1010 have the same numeric value, but the first one says the field is eight bits wide. Dropping those zeros loses information about the field, even though it does not change the arithmetic.
Hexadecimal: the compact way to read bits
Hexadecimal exists because binary is accurate but long, and decimal is short but hides the bits. Each hexadecimal digit corresponds to exactly four bits, so hex is a compact shorthand for binary. The nibble 0000 is 0, 1001 is 9, 1010 is A, 1111 is F, and so on. To read a hex value as bits, expand each digit to its four-bit group. To write bits as hex, group the bits in fours from the right, pad the leftmost group with zeros if needed, and convert each group to one hex digit.
That fixed four-to-one relationship is why hex is the usual base for memory addresses, hash digests, colour codes, UUIDs, byte dumps, and machine code. A colour such as #FF8000 is three bytes – red FF, green 80, blue 00 – and hex makes the three channels visible at a glance. A hash such as a SHA-256 digest is 64 hex digits, which is long enough already; writing it as 256 binary digits would be far worse. Hex is case-insensitive in value, so 0xff and 0xFF are the same number, but keep the casing consistent within a document or codebase to make searching and comparison easier.
ly 0 and 1. Hexadecimal, base 16, uses 0 through 9 plus A through F, where A stands for 10, B for 11, C for 12, D for 13, E for 14, and F for 15.
In every positional system, each digit is multiplied by the base raised to the power of its position, counting from zero on the right. In decimal, 2026 means 2 x 1000 + 0 x 100 + 2 x 10 + 6 x 1. In binary, 101 means 1 x 4 + 0 x 2 + 1 x 1, which is 5 in decimal. In hexadecimal, 0x1F means 1 x 16 + 15 x 1, which is 31 in decimal. Once you see that pattern, conversion stops being a trick and becomes arithmetic.
Programmers often mark the base with a prefix so readers do not guess. Common conventions are 0b for binary, 0x for hexadecimal, and no prefix, or sometimes 0d, for decimal. So 0b1010, 0xA, and 10 all describe the same value. Prefixes are a writing convention, not part of the number itself. A file format or language may use a different convention, such as 0X, # for colours, or a trailing h, so check the context before assuming a prefix means what you expect.
Try a Tool
Free, in your browser — no sign-up.
- .htaccess Generator Open tool →
- Age Calculator Open tool →
- Angle Converter Open tool →
- API Tester Open tool →
- ASCII Converter Open tool →
- Base64 Encoder and Decoder This tool converts text to the Base64 representation used in many transport and test contexts, and it reverses Base64 text back into Unicode text when the input is valid. Open tool →
Related Articles
How to Format XML So It Is Easy to Read
Learn how to format XML so it is easy to read: consistent indentation, one element per line, matched tags, unchanged data, and…
Read article →How to Build a Query String Without Breaking Your Link
Build a query string without breaking your link. Learn ?key=value, the & separator, encoding, a worked example, and why secrets stay out…
Read article →How to Convert Base64 Back to an Image
Learn how to convert Base64 back to an image, read a data URI, decode a 1 by 1 PNG example, check the…
Read article →