Binary to Hex Converter
Binary to hex converter: bits are grouped in fours from the right and each group becomes one hex digit, so 1011110011 is 2F3. Spaces, underscores and a 0b prefix are accepted, and the grouping is shown step by step in your browser.
| Group | Hex digit | Note |
|---|---|---|
| 0010 | 2 | padded with leading zeros |
| 1111 | F | |
| 0011 | 3 |
Binary to hex: group the bits in fours
Converting binary to hexadecimal is a regrouping, not a calculation. Cut the binary number into groups of four bits, starting at the right-hand end, and replace each group with the hex digit of the same value: 0000 to 1001 are 0 to 9 and 1010 to 1111 are A to F. The binary number 1011110011 has ten bits, so the first group has only two of them and gets two zeros in front.
Read the groups left to right and the result is 2F3. If you have the weights in mind (8, 4, 2, 1 for the four positions in a group), each group is added up directly: 1111 is 8 + 4 + 2 + 1 = 15, which is F; 0011 is 2 + 1 = 3.
Why the padding goes on the left
When the number of bits is not a multiple of four, the short group is always the one at the left, and it is padded with zeros on its left. Zeros in front of a number do not change its value, which is why the padding is allowed. Zeros added at the right end would multiply the number by two for each zero and change the answer.
The number 101 is 5. Padded correctly it is 0101, which is hex 5. Padded on the wrong side it would read 1010, hex A, which is 10. A common error comes from cutting from the left: 1011110011 split from the left gives 1011 1100 11, and the last group has to be padded somewhere, which turns 11 into 1100 (C) or 0011 (3) depending on the side. Always start counting at the right.
| Group | Hex digit | Note |
|---|---|---|
| 0010 | 2 | two zeros added on the left |
| 1111 | F | |
| 0011 | 3 |
Common bytes in binary and hex
A few eight-bit patterns appear so often that it pays to recognize them. They split into two groups of four, so each hex digit can be checked separately. Patterns with all bits set or clear in each half are the masks and flags used in bitwise operations; the alternating patterns are used for memory tests and display patterns.
| Binary | Hex | Where it appears |
|---|---|---|
| 00000000 | 00 | Zero; the NUL byte |
| 00001111 | 0F | Mask for the low four bits |
| 11110000 | F0 | Mask for the high four bits |
| 01010101 | 55 | Alternating bits, starting with 0 |
| 10101010 | AA | Alternating bits, starting with 1 |
| 01111111 | 7F | Largest signed byte, 127 |
| 10000000 | 80 | Smallest signed byte, −128; the sign bit alone |
| 11111111 | FF | All bits set, 255 |
What the converter accepts
Binary can be typed with or without spaces, so groups of four or eight bits are accepted as they come from a datasheet or a debugger: 1100 0000 is one number, C0. An 0b prefix, as written in C++14, Python and JavaScript, is allowed, and so is an underscore between groups (1100_0000), which Rust, Java and Python use for readability. Anything other than 0 and 1 produces an error that names the character and its position, so a mistyped 2 or an O instead of a 0 is easy to spot.
Because spaces belong to the number, several values go on separate lines or are separated by commas or semicolons. The output has one result per line in the same order, and a bad entry leaves its line blank without stopping the others. The 0x prefix and lower case output can be switched on in the options row if the result is going into source code.
32-bit and 64-bit binary values
Long strings are where grouping pays off, because hex can be checked a digit at a time. The 32-bit string below is an IPv4 address, 192.168.0.1, written as four bytes. It converts to the eight hex digits C0A80001.
| Group | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
|---|---|---|---|---|---|---|---|---|
| Bits | 1100 | 0000 | 1010 | 1000 | 0000 | 0000 | 0000 | 0001 |
| Hex digit | C | 0 | A | 8 | 0 | 0 | 0 | 1 |
Pairs of groups are bytes: C0 is 192, A8 is 168, 00 is 0 and 01 is 1. The converter takes strings of any length, including the 128-bit values used for IPv6 addresses and UUIDs and the 256-bit values used in cryptography; the result is exact because the number is held as an arbitrary-precision integer. For the reverse direction, hex to binary lists the 4-bit group of each digit.
Binary that spells text
Binary strings found in puzzles and tutorials are usually text, written one byte at a time. The word Hi is 01001000 01101001 in ASCII. Converted as a single number this is 4869 in hex, and 48 69 read as two bytes is the text Hi. If the groups are eight bits wide and every byte starts with a 0 bit, the string is probably ASCII text and is best decoded as text.
The hex that this converter returns can be pasted into the hex decoder to read it as characters. The auto decoder goes a step further: it recognizes a string of ones and zeros whose length is a multiple of 8 and offers the text reading directly.
Signed binary and sign extension
The hex digits do not depend on whether the bits are meant as signed, but the meaning does. The 8-bit pattern 11111111 is FF either way. As an unsigned byte it is 255, as a signed one it is −1. When a value is widened to a longer integer, a signed value is sign-extended: the new top bits copy the old sign bit, so the signed −1 becomes 1111 1111 1111 1111, FFFF, in 16 bits. An unsigned 255 is zero-extended instead and becomes 0000 0000 1111 1111, 00FF.
This is the usual cause of a conversion that looks right in one program and wrong in another: both agree on FF, then one widens it with zeros and the other with ones. The binary form shows the difference immediately, which is a good reason to convert to binary and back when debugging.
Binary to hex in Python, JavaScript and the shell
hex(int("1011110011", 2)) # '0x2f3'
format(int("1011110011", 2), "X") # '2F3'
format(int("11000000101010000000000000000001", 2), "08X") # 'C0A80001'parseInt("1011110011", 2).toString(16) // '2f3'
BigInt("0b1011110011").toString(16) // '2f3' (BigInt accepts 0b)printf '%X\n' $((2#1011110011)) # 2F3
[Convert]::ToString([Convert]::ToInt32("1011110011", 2), 16) # 2f3In every language the conversion goes through an integer, so the usual size limits apply. parseInt is exact for up to 53 bits and Bash arithmetic is 64-bit signed and wraps past 63 bits; for longer strings use BigInt or Python's unbounded integers.
Worked examples: 110101, 1111111111111111 and 8001
110101 has six bits, so the first group is short. Padded to eight it is 0011 0101: 0011 is 3 and 0101 is 5, so the result is 35. In decimal 110101 is 32 + 16 + 4 + 1 = 53, and 35 in hex is 3 × 16 + 5 = 53, which agrees. 1111111111111111 is sixteen ones, four groups of 1111, so it is FFFF, 65,535 in decimal and the largest 16-bit value.
A string written for readability, 1000_0000_0000_0001, has groups already marked by the underscores. The groups 1000, 0000, 0000 and 0001 read 8, 0, 0 and 1, so the hex is 8001. As a signed 16-bit integer this is −32,767, because the top bit is set. Writing the hex next to the binary is how bit patterns are normally documented, and it is the reason the converter ignores underscores and spaces in the input.
Binary fractions in hex
A binary number with a point is grouped outward from the point: left of it in fours counted from the point towards the left, right of it in fours counted from the point towards the right, with zeros added to complete the last group on the right. 101.011 becomes 0101 . 0110, which is 5.6. The fractional digit 6 is 6/16 = 0.375, and the binary 0.011 is 1/4 + 1/8 = 0.375 too. Every hex digit after the point is worth a sixteenth of the digit before it, which is four binary places.
The converter handles whole numbers, and a string with a point is reported as an invalid character. Fractions matter mainly in fixed-point formats, where a 16-bit value such as 0x0180 can mean 1.5 when the point sits eight bits from the right. For floating point, in which the same idea is combined with an exponent, see hex to float.
Reading binary from datasheets and registers
Hardware documentation shows registers as rows of bits with names, and the code that sets them uses hex. Suppose a control register has a four-bit mode field in bits 7 to 4 and four single-bit flags in bits 3 to 0, and you want mode 1010 with flags 0101. Writing the byte in binary gives 1010 0101, the register value is A5, and the hex is a byte-for-byte copy of the bit layout because the field boundary sits on a nibble boundary. Layouts like this are why datasheets prefer 4-bit and 8-bit fields.
Bit numbering is the other trap. Bit 0 is the rightmost, least significant bit, so bit 7 is the leftmost in a byte. Documents that draw registers from bit 7 down to bit 0 match the way the digits are written in hex. When a field does not align to a nibble, for example a 3-bit field in bits 6 to 4, the hex of the whole byte mixes it with its neighbours and you have to go back to the binary to read each part.
Binary literals in programming languages
Source code can contain binary numbers directly, which saves the conversion. The 0b prefix is standard in Python, JavaScript, Java 7 and later, Rust, Go, C++14 and C23, and a compiler turns 0b1011110011 into the same value as 0x2F3. Many languages allow a separator inside long literals to keep them readable: Python, Java, Rust, Go and JavaScript use the underscore (0b1011_1100_11), and C++14 uses the apostrophe (0b0010'1111'0011).
| Base | Literal | Languages |
|---|---|---|
| Binary | 0b1011110011 | Python, JavaScript, Java, Rust, Go, C++14, C23 |
| Hexadecimal | 0x2F3 | C and almost every language that followed it |
| Octal | 0o1363 | Python 3, JavaScript, Rust; a bare leading zero in C |
Choose the form that matches how the value is documented. A mask whose bit positions matter is easier to review in binary; the same mask as a number of eight or more digits is easier in hex. The converter prints either, with an optional prefix, so the line can be pasted straight into a source file.
Bit counts that are not a multiple of eight
Binary strings do not always describe whole bytes. A 9-bit value such as 100000001 groups as 0001 0000 0001, which is 101, a three-digit hex number. If the value is meant to occupy two bytes, pad to 16 bits first, 0000 0001 0000 0001, and write 0101. The two answers are numerically equal and differ only in leading zeros, but a parser that expects whole bytes accepts only the second.
A good rule is to pad to the width of the field the value goes into. Hex written without the padding is correct as a number and risky as data, because the leading zero changes how many bytes a decoder consumes. Where the binary string came from a datasheet, the register width gives the answer; where it is a loose number, the converter's shortest form is what you want.
Subnet masks: binary prefix lengths in hex
A subnet mask is a run of ones followed by zeros, and the length of the run is the prefix length. Written as dotted decimal the mask is hard to read, and in hex it is easy: each group of four ones is an F. A /24 mask is 24 ones and 8 zeros, FFFFFF00, which is the dotted 255.255.255.0. A /26 mask has 26 ones, so the last byte holds two ones and six zeros, 1100 0000 = C0, and the mask is FFFFFFC0, or 255.255.255.192.
| Prefix | Binary of the changing byte | Hex mask | Dotted decimal |
|---|---|---|---|
| /8 | 11111111 | FF000000 | 255.0.0.0 |
| /16 | 11111111 | FFFF0000 | 255.255.0.0 |
| /20 | 11110000 | FFFFF000 | 255.255.240.0 |
| /24 | 11111111 | FFFFFF00 | 255.255.255.0 |
| /25 | 10000000 | FFFFFF80 | 255.255.255.128 |
| /26 | 11000000 | FFFFFFC0 | 255.255.255.192 |
| /28 | 11110000 | FFFFFFF0 | 255.255.255.240 |
| /30 | 11111100 | FFFFFFFC | 255.255.255.252 |
The conversion also works backwards: a mask in hex tells you the prefix length by counting ones. FFFFFFE0 has 27 ones, because E0 is 1110 0000 with three, and the rest are F. A valid mask never has a zero before a one, so a value like FFFF00FF is not a subnet mask.
Verifying a binary to hex conversion
Two checks catch most errors. First, count the bits: after padding, their number must be four times the number of hex digits. Second, compare the first digit with the first bit group: if the number began 1011 1100, the result must begin with B, then C. A conversion that goes wrong usually does it at the padding, so confirm the first group with extra care, and re-run the conversion in the other direction with the hex to binary converter. The result should reproduce the original bit string, apart from leading zeros.
Questions about converting binary to hex
How do I convert binary to hex quickly?
Split the bits into groups of four starting from the right, add zeros on the left of the first group if it is short, and replace each group with its hex digit. 1111 is F, 1010 is A, 0101 is 5. No division or multiplication is needed.
What is 1111 1111 in hex?
FF. Each group of four ones is F, and two groups make one byte, whose value is 255 in decimal.
Why do I get a different answer if I group from the left?
Grouping must start at the right-hand end, because the weights are counted from the right. Binary 101 grouped from the right becomes 0101, which is 5. Grouped from the left it would become 1010 with a stray digit, which is A and wrong.
Can the converter handle several binary numbers at once?
Yes. Put each number on its own line, or separate them with commas. Spaces inside a line group the bits of one number, so 1111 0000 is a single value, F0, and 1111,0000 is two values.
What does a binary number with a minus sign convert to?
The minus sign is kept: −1010 becomes −A. This is a signed magnitude, which is not how computers store negative integers. For the stored two's complement form of a negative number, use the decimal to hex converter.
