How to convert between binary, decimal and hex
Each binary digit, counted from the right, is worth 1, 2, 4, 8, 16, 32, 64, 128 and so on. Add up the places that hold a 1 to get the decimal value: 0100 0001 = 64 + 1 = 65.
To go from decimal to binary, subtract the largest power of two that fits and repeat: 65 = 64 + 1, so bits 7 and 1 are set and the rest are 0.
Hex uses 0–9 and A–F for 0 to 15, which is exactly four binary digits. Split the binary into groups of four from the right and replace each group with one hex digit: 0100 0001 → 4, 1 → 0x41. That’s why firmware and datasheets write register values in hex.
| Decimal | Binary | Hex | Decimal | Binary | Hex |
|---|---|---|---|---|---|
| 0 | 0000 | 0 | 8 | 1000 | 8 |
| 1 | 0001 | 1 | 9 | 1001 | 9 |
| 2 | 0010 | 2 | 10 | 1010 | A |
| 3 | 0011 | 3 | 11 | 1011 | B |
| 4 | 0100 | 4 | 12 | 1100 | C |
| 5 | 0101 | 5 | 13 | 1101 | D |
| 6 | 0110 | 6 | 14 | 1110 | E |
| 7 | 0111 | 7 | 15 | 1111 | F |
Negative numbers and two’s complement
Computers store negative numbers in two’s complement: invert every bit of the positive number and add 1. In 8 bits, -1 is 1111 1111 (0xFF) and -128 is 1000 0000 (0x80). The same bits read as 255 unsigned or -1 signed, depending on how the program declares them.
Once you pick a bit width in the tool above, you can type a negative decimal number, and the result lists the signed value. The range of each width:
| Bits | Unsigned | Signed | C type |
|---|---|---|---|
| 8 | 0 to 255 | -128 to 127 | uint8_t / int8_t |
| 16 | 0 to 65,535 | -32,768 to 32,767 | uint16_t / int16_t |
| 32 | 0 to 4,294,967,295 | -2,147,483,648 to 2,147,483,647 | uint32_t / int32_t |
| 64 | 0 to about 1.8 × 10¹⁹ | about ±9.2 × 10¹⁸ | uint64_t / int64_t |
ASCII table
ASCII uses 0 to 127 for English letters, digits, punctuation and control characters. 0 to 31 and 127 are control characters such as LF (line feed, 0x0A), CR (carriage return, 0x0D) and ESC (0x1B); 32 is the space (SP). A few patterns worth remembering: digits 0–9 are 0x30–0x39, uppercase A is 0x41, lowercase a is 0x61, and the two cases differ by exactly 0x20.
| Dec | Hex | Char | Dec | Hex | Char | Dec | Hex | Char | Dec | Hex | Char |
|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 00 | NUL | 32 | 20 | SP | 64 | 40 | @ | 96 | 60 | ` |
| 1 | 01 | SOH | 33 | 21 | ! | 65 | 41 | A | 97 | 61 | a |
| 2 | 02 | STX | 34 | 22 | " | 66 | 42 | B | 98 | 62 | b |
| 3 | 03 | ETX | 35 | 23 | # | 67 | 43 | C | 99 | 63 | c |
| 4 | 04 | EOT | 36 | 24 | $ | 68 | 44 | D | 100 | 64 | d |
| 5 | 05 | ENQ | 37 | 25 | % | 69 | 45 | E | 101 | 65 | e |
| 6 | 06 | ACK | 38 | 26 | & | 70 | 46 | F | 102 | 66 | f |
| 7 | 07 | BEL | 39 | 27 | ' | 71 | 47 | G | 103 | 67 | g |
| 8 | 08 | BS | 40 | 28 | ( | 72 | 48 | H | 104 | 68 | h |
| 9 | 09 | HT | 41 | 29 | ) | 73 | 49 | I | 105 | 69 | i |
| 10 | 0A | LF | 42 | 2A | * | 74 | 4A | J | 106 | 6A | j |
| 11 | 0B | VT | 43 | 2B | + | 75 | 4B | K | 107 | 6B | k |
| 12 | 0C | FF | 44 | 2C | , | 76 | 4C | L | 108 | 6C | l |
| 13 | 0D | CR | 45 | 2D | - | 77 | 4D | M | 109 | 6D | m |
| 14 | 0E | SO | 46 | 2E | . | 78 | 4E | N | 110 | 6E | n |
| 15 | 0F | SI | 47 | 2F | / | 79 | 4F | O | 111 | 6F | o |
| 16 | 10 | DLE | 48 | 30 | 0 | 80 | 50 | P | 112 | 70 | p |
| 17 | 11 | DC1 | 49 | 31 | 1 | 81 | 51 | Q | 113 | 71 | q |
| 18 | 12 | DC2 | 50 | 32 | 2 | 82 | 52 | R | 114 | 72 | r |
| 19 | 13 | DC3 | 51 | 33 | 3 | 83 | 53 | S | 115 | 73 | s |
| 20 | 14 | DC4 | 52 | 34 | 4 | 84 | 54 | T | 116 | 74 | t |
| 21 | 15 | NAK | 53 | 35 | 5 | 85 | 55 | U | 117 | 75 | u |
| 22 | 16 | SYN | 54 | 36 | 6 | 86 | 56 | V | 118 | 76 | v |
| 23 | 17 | ETB | 55 | 37 | 7 | 87 | 57 | W | 119 | 77 | w |
| 24 | 18 | CAN | 56 | 38 | 8 | 88 | 58 | X | 120 | 78 | x |
| 25 | 19 | EM | 57 | 39 | 9 | 89 | 59 | Y | 121 | 79 | y |
| 26 | 1A | SUB | 58 | 3A | : | 90 | 5A | Z | 122 | 7A | z |
| 27 | 1B | ESC | 59 | 3B | ; | 91 | 5B | [ | 123 | 7B | { |
| 28 | 1C | FS | 60 | 3C | < | 92 | 5C | \ | 124 | 7C | | |
| 29 | 1D | GS | 61 | 3D | = | 93 | 5D | ] | 125 | 7D | } |
| 30 | 1E | RS | 62 | 3E | > | 94 | 5E | ^ | 126 | 7E | ~ |
| 31 | 1F | US | 63 | 3F | ? | 95 | 5F | _ | 127 | 7F | DEL |
FAQ
What does 0x mean in front of a hex number?
It only marks the number as hex and isn’t part of the value: 0x41, 41h and $41 are the same number. C, Python and JavaScript use 0x; assembly often uses an h suffix. Binary is often written with 0b. The tool accepts numbers with or without these prefixes.
What’s the difference between big-endian and little-endian?
When a multi-byte number is stored in memory or sent over a wire, big-endian puts the most significant byte first and little-endian puts the least significant byte first. 0x12345678 is 12 34 56 78 in big-endian and 78 56 34 12 in little-endian. ARM Cortex-M and x86 are little-endian; network protocols and many sensor registers are big-endian. If the bytes on a logic analyzer or UART look reversed, this is usually why.
Can Chinese or accented characters be converted to ASCII?
No. ASCII only has 128 characters. Web pages and most software now use UTF-8, where ASCII characters stay one byte and other characters take 2 to 4 bytes: é is C3 A9 and 你 is E4 BD A0. The tool converts text as UTF-8.
Why is the ASCII code of the digit 0 equal to 48?
ASCII puts the digits 0–9 at 0x30–0x39. A '7' received over UART is the byte 0x37; subtract 0x30 (or '0') to get the number 7. Forgetting this is a common firmware bug.