Base conversion

01 Oct, 2023
BitwiseComputer Science

Base conversion

In the previous publication, I talked about binary numbers and how we can convert from binary to decimal and vice versa. But in computer science, it is common to encounter numbers in other bases, like hexadecimal, octal, etc. So, in this publication, I am going to discuss how we can convert from one base to another.

Numeric systems

  • Binary (Base 2): Uses only two digits: 0 and 1. It is the fundamental language of computers and digital systems.
    • 0, 1
  • Octal (Base 8): Uses digits from 0 to 7. Although less common nowadays, it was used in older systems.
    • 0, 1, 2, 3, 4, 5, 6, 7
  • Decimal (Base 10): The system we use in our daily lives. This is the system most people are familiar with. Contains digits from 0 to 9.
    • 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
  • Hexadecimal (Base 16): Uses digits from 0 to 9 and letters from A to F. It is widely used in programming and system design.
    • 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F

Conversions from decimal to another base

First, we are going to convert from decimal to another base to understand the process.

To convert from decimal to another base, we have to divide the decimal number by the base and get the remainder. Then, we have to divide the result by the base again and get the remainder. We have to repeat this process until the quotient is 0. Then, we have to write the remainders in reverse order.

From decimal to binary

DividendDivisorQuotientRemainderPosition
132614
62303
32112
12011

13(10) = 1101(2)

Python example

print(bin(13)) # 0b1101

From decimal to octal

DividendDivisorQuotientRemainderPosition
54186754
678833
88102
18011

541(10) = 1035(8)

Python example

print(oct(541)) # 0o1035

From decimal to hexadecimal

DividendDivisorQuotientRemainderSymbolPosition
37091623113D3
2311614772
1416014E1

3709(10) = E7D(16)

Python example

print(hex(3709)) # 0xe7d

Algorithm

def fromDecimalTo(num: int, base: int) -> str:
    result = ""

    while num > 0:
        remainder = num % base

        if(remainder > 9):
            result = chr(ord('A') + remainder - 10) + result
        else:
            result = str(remainder) + result

        num //= base

    return result

Conversions from another base to decimal

Now, we are going to convert from another base to decimal to understand the process. To convert from another base to decimal, we have to multiply each digit by the base raised to the power of the position of the digit. Then, we have to add all the results.

From binary to decimal

1101(2) =
1 * 23 + 1 * 22 + 0 * 21 + 1 * 20 =
8 + 4 + 0 + 1 =
13(10)

Python example

print(int('1101', 2)) # 13

From octal to decimal

723(8) =
7 * 82 + 2 * 81 + 3 * 80 =
448 + 16 + 3 =
467(10)

Python example

print(int('723', 8)) # 467

From hexadecimal to decimal

Before we convert from hexadecimal to decimal, we have to convert the letters to numbers. Here is the table with equivalences.

SymbolValue
00
11
22
33
44
55
66
77
88
99
A10
B11
C12
D13
E14
F15

EB9(16) =
14 * 162 + 11 * 161 + 9 * 160 =
3584 + 176 + 9 =
3769(10)

NOTE: When a letter appears, we overwrite it with the equivalent number. E.g., E = 14 and B = 11.

Whit this, we can understand the mathematical formula to convert from another base to decimal is Decimal value = digit_n * b^n + ... + digit_1 * b^1 + digit_0 * b^0

Python example

print(int('EB9', 16)) # 3769

Algorithm

def toDecimal(num: str, base: int) -> int:
    result = 0
    power = 0

    for i in range(len(num) - 1, -1, -1):
        if num[i].isdigit():
            result += int(num[i]) * base ** power
        else:
            result += (ord(num[i]) - ord('A') + 10) * base ** power

        power += 1

    return result

Conclusion

Understanding the conversion between different numerical bases is fundamental in computer science. This knowledge not only allows us to comprehend how computers interpret and manipulate data, but it is also crucial for those who wish to delve deeper into algorithms and data structures, enabling the perception of optimization opportunities that are not possible with more conventional algorithms.

We are using cookies to ensure that we give you the best experience on our website. By clicking "Accept", you consent to the use of ALL the cookies. However you may visit Cookie policy to provide a controlled consent.