EN

Binary, hexadecimal, octal and decimal converter

Any base, signed n-bit numbers, step by step, and text to bytes

Number systems: Binary and hexadecimal Gray code BCD codes Floating and fixed point Parity, Hamming and CRC

Integer converter

Type in any box and the rest update instantly. Numbers of any size are accepted, as well as prefixes such as 0x or 0b and spaces between groups of bits.

Bases
n-bit representation

Bits (click one to flip it)

Unicode character with that code:

Step by step

Text to hexadecimal, binary and Base64

Each character is stored as one or more bytes according to the UTF-8 encoding: ASCII characters (unaccented letters, digits and basic symbols) take one byte, while «ñ», accented vowels or «€» take two or three. Type in any box; in binary and decimal, separate the bytes with spaces.

Text and bytes

What a positional number system is

In a positional system with base \(b\), each digit is worth its value multiplied by a power of the base according to its position, starting with \(b^0\) on the right. A number with digits \(d_{k}\dots d_1 d_0\) is worth:

\[ N = \sum_{i=0}^{k} d_i \cdot b^{\,i} \qquad \text{e.g. } 2026_{10} = 2\cdot10^3 + 0\cdot10^2 + 2\cdot10^1 + 6\cdot10^0 \]

The base tells you how many different digits there are: 10 in decimal (0–9), 2 in binary (0 and 1), 8 in octal (0–7) and 16 in hexadecimal (0–9 and A–F). The base is shown with a subscript (\(101_2\)) or a prefix: 0b101, 0o17, 0x1F (or 1Fh and 16#1F in PLCs).

DecimalBinaryOctalHex
0000000
1000111
2001022
3001133
4010044
5010155
6011066
7011177
81000108
91001119
10101012A
11101113B
12110014C
13110115D
14111016E
15111117F

Binary system

It is the system of digital circuits: each digit (bit) is a 0 or a 1, which the hardware represents with two voltage levels. 4 bits make a nibble, 8 bits a byte and 16 or 32 bits a word (WORD, DWORD). The leftmost bit is the most significant (MSB) and the rightmost the least significant (LSB). With n bits you can count \(2^n\) values, from 0 to \(2^n - 1\).

Octal and hexadecimal

They are compact ways of writing binary: since \(8 = 2^3\) and \(16 = 2^4\), each octal digit equals 3 bits and each hexadecimal digit 4. To convert, just group the bits from the right: \(1011\,0110_2 = \text{B6}_{16}\) and \(10\,110\,110_2 = 266_8\).

Hexadecimal is everywhere: memory addresses, web colours (#1F6FEB), MAC addresses, error codes and Modbus registers. Octal is used mainly in Unix permissions (chmod 755) and in some older PLCs.

Negative numbers: sign and magnitude, one's complement and two's complement

Example: −45 in 8-bit two's complement

\(45 = 0010\,1101\). Invert the bits: \(1101\,0010\). Add 1: \(1101\,0011 = \text{D3}_{16}\). Check: \(-128 + 64 + 16 + 2 + 1 = -45\).

Value (4 bits)Sign and magnitudeOne's complementTwo's complementExcess 8
+70111011101111111
+10001000100011001
00000 and 10000000 and 111100001000
−11001111011110111
−71111100010010001
−8––10000000
Range with n bits\(\pm(2^{n-1}-1)\)\(\pm(2^{n-1}-1)\)\(-2^{n-1} \dots 2^{n-1}-1\)\(-2^{n-1} \dots 2^{n-1}-1\)

If the result of an operation falls outside the range there is an overflow: in 16 bits, 32767 + 1 gives −32768. It is a classic bug in PLC counters of type INT.

Text, ASCII and UTF-8

The ASCII code gives a 7-bit number to every letter, digit and symbol: «A» is 65 (0x41), «a» is 97 (0x61) and «0» is 48 (0x30), so a digit is turned into its value by subtracting 0x30. Codes 0 to 31 are control codes, such as line feed (LF, 0x0A), carriage return (CR, 0x0D) or the STX and ETX that mark the start and end of a frame in many serial devices.

UTF-8 extends ASCII to the whole of Unicode without changing those first 128 codes: the other characters use 2 to 4 bytes, and the first byte says how many follow. That is why a text with accents «takes up more» than it seems, and why odd characters such as «Ã±» appear instead of «ñ» when a device reads those bytes as Latin-1. Base64 is not a text encoding but a way of writing any bytes with 64 printable characters (4 characters for every 3 bytes); it is used in e-mail, certificates and web requests.

Related: Boolean algebra, Karnaugh map and PLC ladder simulator.