Convert Octal to Hexadecimal in C

Advertisements

Prev Tutorial Next Tutorial

Convert Octal to Hexadecimal Program in C

In this types of program we takes a Octal vlaue as input and converts it into Hexadecimal value. To convert Octal value to Hexadecimal vlaue first accept Octal value as a input then convert into equivalent Hexadecimal number.

To convert Octal to hexadecimal first we convert given octal number into binary number then convert these binary number into quivalent hexadecimal number.

Convert Octal to Hexadecimal in C

#include<stdio.h>
#include<conio.h>

int main()
{
    int OCTALVALUES[] = {0, 1, 10, 11, 100, 101, 110, 111};

    long long octal, temp_Octal, binary, place;
    char hex[65] = "";
    int rem;

    place = 1;
    binary = 0;
    clrscr();    

    printf("Please Enter any Octal Number: ");
    scanf("%lld", &octal);
    temp_Octal = octal;

    // First Convert Octal to Binary
     
    while(temp_Octal > 0)
    {
        rem = temp_Octal % 10;
        binary = (OCTALVALUES[rem] * place) + binary;
        temp_Octal /= 10;

        place *= 1000;
    }
    
    // Convert Binary to Hexadecimal
    
    while(binary > 0)
    {
        rem = binary % 10000;
        switch(rem)
        {
            case 0:
                strcat(hex, "0");
                break;
            case 1:
                strcat(hex, "1");
                break;
            case 10:
                strcat(hex, "2");
                break;
            case 11:
                strcat(hex, "3");
                break;
            case 100:
                strcat(hex, "4");
                break;
            case 101:
                strcat(hex, "5");
                break;
            case 110:
                strcat(hex, "6");
                break;
            case 111:
                strcat(hex, "7");
                break;
            case 1000:
                strcat(hex, "8");
                break;
            case 1001:
                strcat(hex, "9");
                break;
            case 1010:
                strcat(hex, "A");
                break;
            case 1011:
                strcat(hex, "B");
                break;
            case 1100:
                strcat(hex, "C");
                break;
            case 1101:
                strcat(hex, "D");
                break;
            case 1110:
                strcat(hex, "E");
                break;
            case 1111:
                strcat(hex, "F");
            break;
        }

        binary /= 10000;
    }

    strrev(hex);

    printf("Octal number: %lld\n", octal);
    printf("Equivalent Hexadecimal number: %s", hex);

  getch();
}

Output 1

Please Enter any Octal Number: 241
Equivalent Hexadecimal Value is: A1

Output 2

Please Enter any Octal Number: 265
Equivalent Hexadecimal Value is: B5

Prev Tutorial Next Tutorial

Google Advertisment

Buy This Ad Space @$20 per Month, Ad Size 600X200 Contact on: hitesh.xc@gmail.com or 9999595223

Magenet is best Adsense Alternative here we earn $2 for single link, Here we get links ads. Magenet

For Projects 9999595223

Google Advertisements


Buy Websites 9999595223

Buy College Projects with Documentation Contact on whatsapp 9999595223. Contact on: hitesh.xc@gmail.com or 9999595223 Try this Keyword C++ Programs

Advertisements