# Binary, hex and decimal converter

> Convert numbers between binary, decimal, hex, octal and any base 2–36, including fractions, repeating digits and two's-complement bits for negatives.

انٹرایکٹو صورت: https://www.calcopenly.com/ur/programming/binary-hex-converter
موضوع: پروگرامنگ اور ٹیکنالوجی کے کیلکولیٹر

A number written in base b is a sum of digits times powers of b, so 2A in hex is 2 × 16 + 10 = 42. To write a whole number in base b, the converter divides by b repeatedly and reads the remainders from last to first; for a fraction it multiplies by b repeatedly and takes each whole part as the next digit. It works with exact fractions, so it can tell whether the digits after the point end or repeat.

Programmers use it for memory addresses, color codes, file permissions and bit masks. The default, −42, is −2A in hex and 1111 1111 1101 0110 in 16-bit two's complement, which reads as 65,494 when the same bits are treated as unsigned. Decimal 0.1 never ends in binary: it is 0.0(0011), with the block 0011 repeating.

Two's-complement bits are shown for whole numbers at widths from 8 to 128 bits; a value outside the chosen width wraps to its low bits, with a warning.

## اندراجات

- **Number**: Use a point for fractions. In auto mode 0x, 0b and 0o prefixes pick the base. Spaces and underscores are ignored.
- **From base** (اختیارات: Auto (prefix, else decimal), Binary (2), Octal (8), Decimal (10), Hex (16), Other base)
- **Source base**
- **To base** (اختیارات: Binary (2), Octal (8), Decimal (10), Hex (16), Other base)
- **Target base**
- **Two's-complement width** (اختیارات: 8-bit, 16-bit, 32-bit, 64-bit, 128-bit)
- **ہندسوں کی گروہ بندی**: Applies to every base except decimal; binary in 4s shows nibbles, in 8s shows bytes. (اختیارات: None, Groups of 4, Groups of 8)
- **Fraction digits to show**

## نتائج

- نتیجہ — بنیادی نتیجہ
- Binary
- Octal
- Decimal
- Hex
- Fraction in the target base
- Exact form
- Repeating block length
- Two's-complement bits
- Two's complement in hex
- Bits read as unsigned
- Bits read as signed

## فارمولا

$$
\begin{aligned} (d_{k-1}\cdots d_1 d_0.d_{-1}d_{-2}\cdots)_b &= \sum_i d_i\, b^{i} \\ -x \text{ in } w \text{ bits} &= 2^w - x\end{aligned}
$$

## حل شدہ مثالیں

### 255 to hex

- Number: 255
- From base: Decimal (10)
- To base: Hex (16)
- Two's-complement width: 16-bit
- ہندسوں کی گروہ بندی: Groups of 4
- **نتیجہ: FF**
- **Binary: 1111 1111**
- **Octal: 377**
- **Fraction in the target base: Whole number**
- جانچ کا ماخذ: ⁨Python 3.8: format(255, 'X'), format(255, 'b'), format(255, 'o')⁩

### −42 in 16-bit two's complement

- Number: -42
- From base: Decimal (10)
- To base: Hex (16)
- Two's-complement width: 16-bit
- ہندسوں کی گروہ بندی: Groups of 4
- **نتیجہ: -2A**
- **Two's-complement bits: 1111 1111 1101 0110**
- **Two's complement in hex: FFD6**
- **Bits read as unsigned: 65494**
- **Bits read as signed: -42**
- جانچ کا ماخذ: ⁨Python 3.8: format(-42 & 0xFFFF, '016b') = '1111111111010110', format(-42 & 0xFFFF, 'X') = 'FFD6', 2**16 − 42 = 65494⁩

### 0.1 decimal is repeating in binary

- Number: 0.1
- From base: Decimal (10)
- To base: Binary (2)
- Two's-complement width: 16-bit
- ہندسوں کی گروہ بندی: None
- Fraction digits to show: 12
- **نتیجہ: 0.000110011001…**
- **Exact form: 0.0(0011)**
- **Fraction in the target base: Repeating**
- **Repeating block length: 4**
- جانچ کا ماخذ: ⁨Python 3.8: format(int(Fraction(1, 10) * 2**12), '012b') = '000110011001'; a separate long-division script finds the remainder cycle of length 4 starting after the first digit⁩

### Hex fraction 0x1.8

- Number: 0x1.8
- From base: Auto (prefix, else decimal)
- To base: Decimal (10)
- Two's-complement width: 16-bit
- ہندسوں کی گروہ بندی: Groups of 4
- **نتیجہ: 1.5**
- **Fraction in the target base: Terminating**
- **Binary: 1.1**
- جانچ کا ماخذ: ⁨Python 3.8: float.fromhex('0x1.8') = 1.5⁩

### 128 overflows signed 8-bit

- Number: 128
- From base: Decimal (10)
- To base: Binary (2)
- Two's-complement width: 8-bit
- ہندسوں کی گروہ بندی: None
- **نتیجہ: 10000000**
- **Two's-complement bits: 10000000**
- **Bits read as unsigned: 128**
- **Bits read as signed: -128**
- جانچ کا ماخذ: ⁨Python 3.8: int.from_bytes(bytes([128]), 'big', signed=True) = -128⁩

### Base 36 ZZ

- Number: ZZ
- From base: Other base
- Source base: 36
- To base: Decimal (10)
- Two's-complement width: 16-bit
- ہندسوں کی گروہ بندی: Groups of 4
- **نتیجہ: 1295**
- **Hex: 50F**
- جانچ کا ماخذ: ⁨Python 3.8: int('ZZ', 36) = 1295, format(1295, 'X') = '50F'⁩

## سوالات

### How do you convert binary to decimal?

Multiply each binary digit by 2 raised to its position, counting from 0 at the right, and add the results. 1111 1111 is 128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = 255, and 101010 is 32 + 8 + 2 = 42. Digits after a binary point use negative powers, so 1.1 is 1 + 1/2 = 1.5.

### How do you convert decimal to hexadecimal?

Divide by 16 repeatedly and write the remainders from last to first, using A–F for 10–15. For 255, 255 ÷ 16 = 15 remainder 15, then 15 ÷ 16 = 0 remainder 15, giving FF; for 1295 the remainders 15, 0 and 5 give 50F. Each hex digit stands for exactly 4 bits, which is why hex is used to write bytes: FF is 1111 1111.

### What is two's complement?

Two's complement is how CPUs store signed integers: a negative number −x in w bits is stored as 2^w − x, so the top bit carries a weight of −2^(w−1). In 16 bits, −42 is 65,536 − 42 = 65,494, or FFD6 in hex. An 8-bit value runs from −128 to 127, so 128 wraps to −128. C23 (ISO/IEC 9899:2024) requires two's complement for signed integers.

### Why is 0.1 not exact in binary?

A fraction ends in base b only if its denominator, in lowest terms, has no prime factors other than those of b. 0.1 is 1/10, and 10 = 2 × 5, so in binary it repeats forever: 0.000110011…, written 0.0(0011). That is why 0.1 + 0.2 gives 0.30000000000000004 in IEEE 754 double precision, the number type behind JavaScript numbers and Python floats.

### What is octal used for?

Octal (base 8) groups binary digits in threes, and its main use today is Unix file permissions: each digit adds read (4), write (2) and execute (1) for the owner, group and others, so chmod 755 means rwxr-xr-x. Decimal 255 is 377 in octal. Python 3 and JavaScript write octal literals as 0o755; C writes them with a leading zero, 0755.

### “⁨Binary, hex and decimal converter⁩” کتنا درست ہے؟

درستی آپ کی درج کردہ قدروں اور طریقے کے مفروضوں پر منحصر ہے۔ اعشاری حساب 50 بامعنی ہندسے استعمال کرتا ہے، مگر تخمینے، عددی طریقے اور ماخذ کا ڈیٹا کم درست ہو سکتے ہیں؛ دکھائی گئی قدروں کو راؤنڈ کرنے سے یہ حدود ختم نہیں ہوتیں۔ آزاد ذرائع کی حل شدہ مثالوں سے جانچ: 7۔ مثلاً، “⁨255 to hex⁩” کو ⁨Python 3.8: format(255, 'X'), format(255, 'b'), format(255, 'o')⁩ سے جانچا جاتا ہے۔

### اس طریقے کا ماخذ کیا ہے؟

Knuth, The Art of Computer Programming, Vol. 2, §4.1 Positional number systems and §4.4 Radix conversion; ISO/IEC 9899:2024 (C23) §6.2.6.2 — signed integers use two's complement (draft N3096).

## ماخذ

- Knuth, The Art of Computer Programming, Vol. 2, §4.1 Positional number systems and §4.4 Radix conversion
- [ISO/IEC 9899:2024 (C23) §6.2.6.2 — signed integers use two's complement (draft N3096)](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3096.pdf)
