Convert a number to 8-bit binary

How 8-bit binary conversion works, where it fails (range, sign, leading zeros), and when decimal or hex is the better choice for sharing a value.

Is 8-bit binary the right format for your use?

Writing a number as eight binary digits suits teaching, bit-level work (flags, masks, register values, network octets), and any case where a reader needs to see each bit's position. The fixed width lines up values and makes individual bits easy to compare. The tradeoff is that the format only holds a small range of values, and the result is a fragile text string: software can strip its leading zeros, and people read it slowly. If readers only need the quantity, plain decimal is clearer. If they need a compact byte value, hexadecimal (two digits per byte) is usually easier to read and copy than eight binary digits.

What the conversion actually changes

This is not a reversal, mirroring or font effect. It replaces the characters themselves: the decimal digits of the input become a new string made only of the characters 0 and 1. Nothing is applied through styling or display, so what you copy is exactly the new string. Note that this string is a text representation of a byte, not the byte itself. Stored as text, "00101010" is eight characters, which takes eight bytes in ASCII or UTF-8, not one.

Each of the eight positions stands for a power of two. From left to right the place values are 128, 64, 32, 16, 8, 4, 2 and 1. To convert by hand, work from the left: write 1 and subtract wherever the place value fits into what remains, and write 0 wherever it does not. For example, 42 is 32 + 8 + 2, which gives 00101010.

Clarify which conversion you mean

  • Number to binary: a single value such as 200 becomes one 8-bit pattern. This article covers this case.
  • Text to binary: each character of a word is encoded (for example, as ASCII or UTF-8 bytes), and each byte is written as eight bits. The letter A becomes 01000001, which is the code for A, not a numeric value you typed. Characters outside ASCII take more than one byte in UTF-8, so the output is not always eight bits per character.
  • Unsigned or signed: an unsigned 8-bit value covers 0 to 255. A signed value in two's complement covers -128 to 127. The same eight bits can mean different numbers depending on which convention the reader assumes.

Worked examples

Decimal input | 8-bit binary | Note
5 | 00000101 | Padded with leading zeros to eight digits
42 | 00101010 | 32 + 8 + 2
127 | 01111111 | Largest signed 8-bit value
200 | 11001000 | 128 + 64 + 8; unsigned only
255 | 11111111 | Largest unsigned 8-bit value
-5 | 11111011 | Two's complement; the same bits read as 251 unsigned
-128 | 10000000 | Smallest signed 8-bit value
256 | does not fit | Needs nine bits (100000000)

For negative numbers in two's complement, write the positive value in eight bits, flip every bit, then add one. For -5: 00000101 becomes 11111010, then 11111011.

Range, padding and truncation

The most common failure is a value outside the range. Tools handle this in different ways. Some refuse the input, some return more than eight digits, and some silently keep only the lowest eight bits, which turns 256 into 00000000 without warning. Fractions and non-integers have no standard 8-bit integer form at all. Before you rely on a converter, test it with 255, 256 and a negative number, and check which of these behaviors it uses.

In code, the standard formatting functions set a minimum width, not a maximum, so they pad but never truncate. Masking with 0xFF gives the two's complement pattern for negatives. It also silently discards higher bits, so check the range first if overflow matters.

# Python
format(42, '08b')         # '00101010'
format(256, '08b')        # '100000000' (nine digits, not truncated)
format(-5, '08b')         # '-0000101'  (sign plus magnitude, not two's complement)
format(-5 & 0xFF, '08b')  # '11111011'

// JavaScript
(42).toString(2).padStart(8, '0')         // "00101010"
((-5) & 0xFF).toString(2).padStart(8, '0') // "11111011"

Will the result stay readable and usable after copying?

Because the output is ordinary digit characters, it copies and pastes as plain text without encoding issues. The risk comes from destinations that read it as a number. Spreadsheets and similar data tools often treat a pasted string of digits as a numeric value, which can drop the leading zeros (00000101 becoming 101) or even read it as one hundred one in decimal. Formatting the cell or field as text, or adding a prefix such as 0b, usually avoids this. No destination-specific tests were supplied here, so check the specific application you plan to use.

Screen readers may announce an 8-digit string as one large number or digit by digit, depending on the software and its settings. Neither is easy to follow. No assistive-technology documentation or test results were supplied, so this behavior is not verified. Where binary is shown to a general audience, give the decimal value next to it (for example, "00101010 (42)"). Grouping the bits into two sets of four (0010 1010) can also help visual readers, though the space may break parsing in tools that expect eight unbroken digits.

For search and editing, a binary string is only findable as that exact string. Someone searching for 42 will not find 00101010. Editing by hand is also error-prone, because a single flipped or missing digit changes the value without any obvious sign. Keep the decimal or hexadecimal original whenever the value has to be searched, edited or verified later.

Before you choose a converter

No specific tool was evaluated for this article, so nothing here describes a particular converter's features, accuracy or data handling. A tool's design or marketing language says nothing reliable about whether it stores or logs what you enter. If the numbers are sensitive, convert them locally, for example with the code above.