Convert binary numbers to hexadecimal

How to convert binary to hexadecimal and decimal, worked through with 00101001 (0x29, 41) and 11011 (0x1B, 27), plus Java code and notes on labeling and copying results.

Binary-to-hexadecimal conversion rewrites a number in a different base without changing its value. This review works through the main example, 00101001, into both hexadecimal and decimal. It then covers further examples and Java code, and explains what to watch for when the result is copied, shared or read by someone else.

Quick answer: 00101001 in hexadecimal and decimal

Binary 00101001 equals hexadecimal 29 and decimal 41. Hexadecimal 29 is often written as 0x29 to show that it is not the decimal number twenty-nine.

  • Split the eight bits into two groups of four: 0010 and 1001.
  • Convert each group: 0010 is 2, and 1001 is 9.
  • Join the hex digits: 29 (hex).
  • For decimal, add the place values of the 1 bits: 32 + 8 + 1 = 41.
  • Cross-check: 2 × 16 + 9 = 41, so the hex and decimal results agree.

Is this conversion suitable for your use?

Hexadecimal suits technical settings such as memory addresses, byte values, color codes, bit masks and debugging output. Its main benefit is compactness that still maps exactly to the bits: each hex digit stands for exactly four binary digits. You can therefore read the bit pattern back from the hex with no arithmetic beyond a 16-entry lookup.

The main tradeoff is that the reader must know which base is in use. A bare "29" is ambiguous, and readers unfamiliar with hex will not see that it means forty-one. Plain decimal is the better choice for general audiences, quantities, prices, counts, and anything people will compare or calculate by hand. Keep binary itself when individual bit positions matter, for example when documenting flags.

What the conversion actually changes

Only the notation changes. The stored characters are rewritten from the digits 0 and 1 into the digits 0–9 and letters A–F, but the numeric value is the same. This is a real change of characters, not a visual effect like a font, a mirrored glyph or CSS styling. If you paste the output somewhere, you are pasting different characters from the input.

Some terms used near this topic mean something else. "Reversing" a binary number usually refers to bit reversal, which reorders the bits and produces a different value. Byte order (endianness) is likewise a separate concern about how multi-byte values are laid out. Base conversion does neither: 00101001 becomes 29, not 94 or any other reordering.

How converting from binary to hexadecimal works

Because 16 is 2 to the fourth power, every group of four bits (a nibble) maps to exactly one hex digit. Start from the rightmost bit and group leftward in fours. If the leftmost group is short, pad it with leading zeros, then replace each group using the table below.

Binary | Hex | Decimal
0000 | 0 | 0
0001 | 1 | 1
0010 | 2 | 2
0011 | 3 | 3
0100 | 4 | 4
0101 | 5 | 5
0110 | 6 | 6
0111 | 7 | 7
1000 | 8 | 8
1001 | 9 | 9
1010 | A | 10
1011 | B | 11
1100 | C | 12
1101 | D | 13
1110 | E | 14
1111 | F | 15

Worked example: the binary number 11011

11011 has five bits, so grouping from the right gives 1 and 1011. Pad the short group to 0001. Then 0001 is 1 and 1011 is B, so 11011 in hexadecimal is 1B. In decimal it is 16 + 8 + 2 + 1 = 27, which matches 1 × 16 + 11 = 27.

Grouping must start from the right. Grouping 11011 from the left as 1101 and 1 would give the wrong answer, D1.

More examples of binary to hexadecimal conversion

Binary | Grouped | Hex | Decimal
1010 | 1010 | A | 10
11011 | 0001 1011 | 1B | 27
00101001 | 0010 1001 | 29 | 41
10100011 | 1010 0011 | A3 | 163
11111111 | 1111 1111 | FF | 255
110010101 | 0001 1001 0101 | 195 | 405

Each row was checked by computing the decimal value from both the binary and the hex. For example, 110010101 is 256 + 128 + 16 + 4 + 1 = 405, and hex 195 is 1 × 256 + 9 × 16 + 5 = 405.

Converting binary to hexadecimal in Java

The usual approach in Java has two steps. First parse the binary string as base 2, then format the resulting number as hex. The standard library methods below cover the common cases.

String bin = "00101001";
int value = Integer.parseInt(bin, 2);          // 41
String hex = Integer.toHexString(value);       // "29"
String padded = String.format("%02X", value);  // "29", uppercase, at least 2 digits

// Longer inputs: arbitrary length, no overflow
String big = new java.math.BigInteger("110010101", 2).toString(16);  // "195"
  • Integer.toHexString returns lowercase letters and drops leading zeros. For example, 00001111 becomes "f" rather than "0F". Use String.format with a width such as %02X when the digit count matters.
  • Integer.parseInt with radix 2 accepts only values that fit in a signed int. A string of 32 ones throws NumberFormatException; use Integer.parseUnsignedInt, Long.parseLong or BigInteger for wider values.
  • Integer.toHexString on a negative int prints its 32-bit two's-complement form. For example, -1 becomes "ffffffff".
  • parseInt does not accept a 0b prefix, spaces or underscores, so strip them before parsing. Underscores and the 0b prefix are allowed only in binary literals written directly in source code, such as 0b0010_1001.

Readability and usability after copying or sharing

Converted values are made of ordinary ASCII digits and letters, so they generally copy and paste as plain text. The practical risks come from missing context and formatting choices rather than from the characters themselves.

  • Label the base. Write 0x29, 29h or "29 (hex)" instead of a bare 29, and 0b00101001 for binary, so a reader or program does not treat the value as decimal.
  • Keep leading zeros when width carries meaning. 0x09 and 0x9 have the same value, but a fixed two-digit form signals a full byte.
  • Prefer plain-text labels over Unicode subscripts such as ₁₆. Subscript characters are different code points from ordinary digits, so they may not match searches, may be rejected by parsers, and may render inconsistently.
  • Remove grouping spaces ("0010 1001") before pasting into a tool or program that expects a continuous string, unless the destination documents that it accepts them.
  • Be consistent with letter case. Upper- and lowercase hex are equivalent in value, but mixing them makes text harder to scan and to search.

Some questions are not covered by the material supplied for this review. No documentation or test results were available on how specific screen readers announce hex values such as 1B or 0x29. A reader might hear them as words, letters or digit by digit, and that may vary by software and settings. If the audience includes assistive-technology users or non-specialists, give the decimal equivalent alongside the hex, for example "0x29 (41)". That plain-text form is unambiguous in any environment.