Base Conversion

Base Conversion

Introduction

Number System

The number system is the system of naming or representing numbers. Number systems are the technique to represent numbers in the computer system architecture.

Number system conversion is the process of converting one number system to another. In other words it is the conversion of number of one base to number of another base. Example : Binary to Decimal, Octal to Hexadecimal etc.

There are four number systems:

  1. Binary Number system
  2. Decimal Number system
  3. Octal Number system
  4. Hexadecimal (Hex) Number system

Binary Number system

A Binary number system has only two digits i.e 0 and 1 by which all the digital system performs. The base of binary number system is 2. Machine language is written only using binary codes.

  1. Take the lsb of the binary number.
  2. Multipy lsb with 2^0, likewise for previous lsb multiply with 2^1 and goes on.
  3. Add all the multiplied values to get the equivalant decimal number.
C Code:

int binaryToDecimal(string n)
{
    string num = n;
    int dec_value = 0;
    int base = 1;
    
    int len = num.length();
    for (int i = len - 1; i >= 0; i--) {
        if (num[i] == '1')
            dec_value += base;
        base = base * 2;
    }
    
    return dec_value;
}                                
  1. Convert the binary number to decimal number.
  2. Now convert this decimal value to octal number.
C Code:

int binaryToDecimal(string n)
{
    string num = n;
    int dec_value = 0;
    int base = 1;
    
    int len = num.length();
    for (int i = len - 1; i >= 0; i--) {
        if (num[i] == '1')
            dec_value += base;
        base = base * 2;
    }
    
    decToOctal(dec_value);
}
int decToOctal(int n)
{
    int octalNum[100];
    int i = 0;
    while (n != 0) {
        octalNum[i] = n % 8;
        n = n / 8;
        i++;
    }
    for (int j = i - 1; j >= 0; j--){
        printf("%d",octalNum[j]);
    }
} 
  1. Start from the extreme right and group the binary digits in groups of four.
  2. Add zeros at the left of the binary number if the leftmost group has less than 4 binary digits.
  3. Represent each binary group by its hex equivalent number from the table.
  4. Concatenate all the hex digits together to get the converted answer.
C Code:

int BinaryToHexadecimal(binary)
{
    int hexConstant[] = {0, 1, 10, 11, 100, 101, 110, 111, 1000, 
                         1001, 1010, 1011, 1100, 1101, 1110, 1111};

    long long binary, tempBinary;
    char hex[20];
    int index, i, digit;
    
    tempBinary = binary;
    index = 0;
    
    while(tempBinary!=0)
    {
        digit = tempBinary % 10000;
        for(i=0; i<16; i++)
        {
            if(hexConstant[i] == digit)
            {
                if(i<10)
                {
                    hex[index] = (char)(i + 48);
                }
                else
                {
                    hex[index] = (char)((i-10) + 65);
                }

                index++;
                break;
            }
        }

        tempBinary /= 10000;
    }
    hex[index] = '\0';
    for(int i=strlen(hex)-1;i>=0;i--){
        printf("%c",hex[i]);
    }
}

Decimal Number system

The Decimal number system uses the values from 0 to 9. The base of decimal system is represented by 10.

  1. Divide the given decimal number by 2.
  2. Note down the remainder and divide the quotient untill it becomes 0 or 1.
  3. Now write the remainders from bottom to top.
C Code:

int decToBinary(int n)
{
    int binaryNum[32];
 
    int i = 0;
    while (n > 0) {
        binaryNum[i] = n % 2;
        n = n / 2;
        i++;
    }
    for (int j = i - 1; j >= 0; j--){
        printf("%d",binaryNum[j]);
    }
}
  1. If the given decimal number is less than 8 the octal number is the same.
  2. If the decimal number is greater than 7 then divide the number by 8.
  3. Note the remainder, we get after division
  4. Repeat step 2 and 3 with the quotient till it is less than 8 and write the remainders in reverse order.
C Code:

int decToOctal(int n)
{
    int octalNum[100];
    int i = 0;
    while (n != 0) {
        octalNum[i] = n % 8;
        n = n / 8;
        i++;
    }
    for (int j = i - 1; j >= 0; j--){
        printf("%d",octalNum[j]);
    }
}
  1. Divide the decimal number by 16 and note the remainder.
  2. Repeat step 1 with the quotient till it is 0.
  3. The hex value is the digit sequence of the remainders from the last to first
  4. Replace hex values on the result. i.e. 10 as A, 11 as B and so on.
C Code:

void decToHexa(int n)
{
    char hexaDeciNum[100];
    int i = 0;
    while (n != 0) {
        int temp = 0;
 
        temp = n % 16;
 
        if (temp < 10) {
            hexaDeciNum[i] = temp + 48;
            i++;
        }
        else {
            hexaDeciNum[i] = temp + 55;
            i++;
        }
 
        n = n / 16;
    }
 
    for (int j = i - 1; j >= 0; j--)
        printf("%d",hexaDeciNum[j]);
}

Hexadecimal Number system

A Hexadecimal number system has sixteen (16) alphanumeric values from 0 to 9 and A to F. Each digit of hexadecimal number system can be represented as 4 digits known as "Hex digit" or "Nibble". The base of hexadecimal number system is 16.

  1. Write down the hex number and represent each hex digit by its binary equivalent number.
  2. Use 4 digits and add insignificant leading zeros if the binary number has less than 4 digits.
  3. Then concatenate all the digits together to get equivalant binary number.
C Code:

void hextobin(char hex[])  
{  
   int i=0;   
   while(hex[i])  
   {  
       switch(hex[i])  
       {  
           case '0':  printf("0000");  break;  
           case '1':  printf("0001");  break;  
           case '2':  printf("0010");  break;  
           case '3':  printf("0011");  break;  
           case '4':  printf("0100");  break;  
           case '5':  printf("0101");  break;  
           case '6':  printf("0110");  break;  
           case '7':  printf("0111");  break;  
           case '8':  printf("1000");  break;  
           case '9':  printf("1000");  break;  
           case 'A':  printf("1010");  break;  case 'a':  printf("1010");  break;  
           case 'B':  printf("1011");  break;  case 'b':  printf("1011");  break;  
           case 'C':  printf("1100");  break;  case 'c':  printf("1100");  break;  
           case 'D':  printf("1101");  break;  case 'd':  printf("1101");  break;  
           case 'E':  printf("1110");  break;  case 'e':  printf("1110");  break;  
           case 'F':  printf("1111");  break;  case 'f':  printf("1111");  break;  
       }  
       i++;  
}}  
  1. Convert the hex value to its decimal form.
  2. Then convert the decimal value to Binary.
C Code:

int hexadecimal_to_decimal(int x)
{
        int decimal_number, remainder, count = 0;
        while(x > 0)
        {
            remainder = x % 10;
            decimal_number = decimal_number + remainder * pow(16, count);
            x = x / 10;
            count++;
        }
        decToBinary(decimal_number);
}
int decToBinary(int n)
{
    int binaryNum[32];
 
    int i = 0;
    while (n > 0) {
        binaryNum[i] = n % 2;
        n = n / 2;
        i++;
    }
    for (int j = i - 1; j >= 0; j--){
        printf("%d",binaryNum[j]);
    }
}
  1. Write the given Hex value.
  2. Multipy lsb with 16^0, likewise for previous lsb multiply with 16^1 and goes on.
  3. Add all the multiplied values to get the equivalant decimal number.
C Code:

int hexadecimal_to_decimal(int x)
{
        int decimal_number, remainder, count = 0;
        while(x > 0)
        {
            remainder = x % 10;
            decimal_number = decimal_number + remainder * pow(16, count);
            x = x / 10;
            count++;
        }
        return decimal_number;
}

Octal Number system

Octal number system has 8 digits from 0 to 7. The base of octal number system is 8.

  1. Convert the octal number to its decimal equivalent.
  2. Now convert this decimal value to binary.
C Code:

int octalToDecimal(int n)
{
    int num = n;
    int dec_value = 0;
    
    int base = 1;
    
    int temp = num;
    while (temp) {
    
        int last_digit = temp % 10;
        temp = temp / 10;
        dec_value += last_digit * base;   
        base = base * 8;
    }  
    decToBinary(dec_value);
}
int decToBinary(int n)
{
    int binaryNum[32];
 
    int i = 0;
    while (n > 0) {
        binaryNum[i] = n % 2;
        n = n / 2;
        i++;
    }
    for (int j = i - 1; j >= 0; j--){
        printf("%d",binaryNum[j]);
    }
}
  1. Multiply each digit of octal number with its increasing power of 8 from right to left.
  2. Add all the individual results provides the equivalent decimal number.
C Code:

int octalToDecimal(int n)
{
    int num = n;
    int dec_value = 0;
    
    int base = 1;
    
    int temp = num;
    while (temp) {
    
        int last_digit = temp % 10;
        temp = temp / 10;
        dec_value += last_digit * base;   
        base = base * 8;
    }  
    return dec_value;
}
  1. Convert the octal value to decimal number.
  2. Now convert the decimal value to hexadecimal value.
C Code:

int octalToDecimal(int n)
{
    int num = n;
    int dec_value = 0;
    
    int base = 1;
    
    int temp = num;
    while (temp) {
    
        int last_digit = temp % 10;
        temp = temp / 10;
        dec_value += last_digit * base;   
        base = base * 8;
    }  
    decToHexa(dec_value);
}
void decToHexa(int n)
{
    char hexaDeciNum[100];
    int i = 0;
    while (n != 0) {
        int temp = 0;
 
        temp = n % 16;
 
        if (temp < 10) {
            hexaDeciNum[i] = temp + 48;
            i++;
        }
        else {
            hexaDeciNum[i] = temp + 55;
            i++;
        }
 
        n = n / 16;
    }
 
    for (int j = i - 1; j >= 0; j--)
        printf("%d",hexaDeciNum[j]);
}