Supports radix conversion for integers of any bit length. If you need to convert integers, use this.

Radix, as the name suggests, is the method of carrying over when counting. For example, decimal is base ten, meaning it carries over every ten; binary is base two, carrying over every two, and so on.

The most commonly used in our daily lives is decimal. Other commonly used radix units include binary, octal, and hexadecimal.

Decimal is the most commonly used radix unit, passed down since our monkey ancestors. This is because everyone has ten fingers, and naturally, fingers were used as human counters.

Decimal is easy to understand. It uses ten Arabic numerals from 0 to 9, carrying over every ten.

Binary is represented by two digits 0 and 1, carrying over every two. For example, in binary, 1 plus 1 equals 2, which carries over to become binary 10.

Binary is commonly used in computers because the physical structure of computers uses high and low voltages, which can be represented by 0 and 1.

Octal is made up of eight digits from 01234567, carrying over every eight. The calculation process is similar to binary.

Hexadecimal consists of sixteen characters from 0123456789abcdef. Hexadecimal values generally have a 0x prefix, such as 0x8a, representing the hexadecimal value 8a.

So how do you calculate to convert a number to another radix? Let's consider converting a decimal to N radix and converting N radix to decimal (N can be any radix, such as 2, 8, or 16).

Converting Decimal Value to N Radix

- Assume the decimal value is D1
- Divide D1 by N, take the remainder as the value of the first digit (lowest digit) in base N
- Divide D1 by N and round down the result as the decimal value for the next round of calculation, denoting this value as D2
- Divide D2 by N, take the remainder as the value of the second digit in base N
- Divide D2 by N and round down the result as the decimal value for the next round of calculation, denoting this value as D3
- Repeat the above logic until the result of dividing Dx by N and rounding down is 0

For example, to convert the decimal number 19 to binary using the above formula, the steps are

- 19 % 2 = 1, use 1 as the lowest digit of the binary value; 19 / 2 = 9.5, round down to 9
- 9 % 2 = 1, use 1 as the second digit; 9 / 2 = 4.5, round down to 4
- 4 % 2 = 0, use 0 as the third digit; 4 / 2 = 2
- 2 % 2 = 0, use 0 as the fourth digit; 2 / 2 = 1
- 1 % 2 = 1, use 1 as the fifth digit; 1 / = 0.5, round down to 0, process ends
- The final binary value is 10011

Converting base N values to decimal

- Assume a binary value of B1
- Starting from the lowest digit, multiply each digit of B1 by N raised to the power of (digit position - 1)
- Add up each product from step 2 to get the decimal value

For example, to convert the binary value 1101 to decimal, the steps are

- 1101 has 4 digits, starting from the lowest, the first digit is 1, the second is 0, the third is 1, the fourth is 1
- The calculation for the first digit is 1 * 2 ^ (1 - 1) = 1 * 2 ^ 0 = 1 * 1 = 1, where 2 ^ 0 means 2 to the power of 0
- The calculation for the second digit is 0 * 2 ^ (2 - 1) = 0
- The calculation for the third digit is 1 * 2 ^ (3 - 1) = 4
- The calculation for the fourth digit is 1 * 2 ^ (4 - 1) = 8
- The decimal conversion result is 1 + 0 + 4 + 8 = 13

To convert between other bases, use the above method to first convert to decimal, then to the target base