Convert Octal to Hexadecimal Program in C++

Google Advertisements

Prev Tutorial Next Tutorial

C++ program to Convert Octal to Hexadecimal

To convert Octal number to Hexadecimal number in C++ programming, We have to ask to the user to enter the Octal number to convert it into Hexadecimal number to display the equivalent value in Hexadecimal format on screen.
When converting from octal to hexadecimal, it is often easier to first convert the octal number into binary and then from binary into hexadecimal.

Hex / decimal / octal / binary conversion table

Hex Decimal Octal Binary
0 0 0 0
1 1 1 1
2 2 2 10
3 3 3 11
4 4 4 100
5 5 5 101
6 6 6 110
7 7 7 111
8 8 10 1000
9 9 11 1001
A 10 12 1010
B 11 13 1011
C 12 14 1100
D 13 15 1101
E 14 16 1110
F 15 17 1111
10 16 20 10000
20 32 40 100000
40 64 100 1000000
80 128 200 10000000
100 256 400 100000000
200 512 1000 1000000000
400 1024 2000 10000000000

Example

Input Octal Number : 10
Euivalent Hexadecimal Number : 8

Input Octal Number : 6
Euivalent Hexadecimal Number : 6

Input Octal Number : 11
Euivalent Hexadecimal Number : 9

C++ program to convert Octal to Hexadecimal

#include<iostream.h>
#include<conio.h>
#include<math.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();    

    cout<<"Please Enter any Octal Number: ";
    cin>>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);

    cout<<" Equivalent Hexadecimal number: "<< hex;

  getch();
}

Output

Enter any Octal Number: 15
Equivalent Hexadecimal Value is: D
c++ program to convert octal to hexadecimal
Prev Tutorial Next Tutorial

Google Advertisements

We have more than 5 Groups which have 500000+ Members and some Pages have more than 100000+ members. For Advertisement contact on hitesh.xc@gmail.com or 9999595223

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

Pure VPN Privide Lowest Price VPN Just @ $1.65. Per Month with Non Detected IP Lowest Price Non Detected IP VPN

Classpath variable is set for providing a path for predefined Java classes which is used in our application. All classes are available in lib/rt.jar so we set classpath upto lib/rt.jar. Difference between path and classpath in Java

Cloud computing is the on demand availability of computer system resources, especially data storage and computing power, without direct active management by the user. Cloud Computing Tutorial

College Projects Related to Java, AWT, C Projects for College, C++ Projects for College, Android Projects. Download Java C C++ Projects

Download Projects

Adsense Advertisements


Buy This ads Space 8076671483

Buy This Ad Space @$120 per Month, Ad Size 300X600 Contact on: hitesh.xc@gmail.com or 8076671483 Try this Keyword C++ Programs