×

Decimal to Hexadecimal in C++

We need to write a program in C++ that converts a decimal number into an equal hexadecimal number given a decimal value as input i.e. convert a number having a base value of 10 to a number with a base value of 16.

A number in hexadecimal format is represented by 16 values. Numbers 0-9 are represented by digits 0-9, and numbers 10 to 15 are represented by letters A to F.

Let see an example of a program in C++ to convert decimal to hexadecimal number:

// C++ program to convert a decimal
// number to hexadecimal number
#include < iostream >
#include < bits/stdc++.h >
#include < stdlib >
using namespace std ; 
// function to convert decimal to hexadecimal
void decToHexadecimal ( int n )
{
	// char array to store hexadecimal number
	char hexaDeciNumber [ 100 ] ; 
	// counter for hexadecimal number array
	int i = 0 ;
	while ( n != 0 ) {
		// temporary variable to store remainder
		int temp = 0 ; 
		// storing remainder in temp variable.
		temp = n % 16 ; 
		// check if temp < 10
		if ( temp < 10 ) {
			hexaDeciNumumber [ i ] = temp + 48 ;
			i++ ;
		}
		else {
			hexaDeciNumber [ i ] = temp + 55 ;
			i++ ;
		}
		n = n / 16 ;
	}
	// printing hexadecimal number array in reverse order
	for ( int j = i - 1 ; j >= 0 ; j-- )
		cout << hexaDeciNumber [ j ] ;
}
// Driver program to test above function
int main ( )
{
	int n = 3242 ; 
	decToHexadecimal ( n ) ; 
	return 0 ;
}

OUTPUT:

CAA
……
Process executed in 0.11 seconds
Press any key to continue

Explanation:

When an integer is divided by 16, the residue is stored in a temporary variable called temp. If the temperature is less than 10, put (48 + temp) in a character array; otherwise, put (55 + temp) in the character array if the temperature is more than or equal to 10. Now multiply the number by 16 to get the answer. Repeat steps 1 and 2 until the number is not equal to 0. Now reverse the order of the array.

Let us look at an example using pre defined function in C++:

// C++ program to convert decimal number to hexadecimal
#include <iostream>
using namespace std; 
// function to convert decimal number to hexadecimal
void decToHexa(int n) { cout << hex << n << endl; }
// driver code
int main()
{
	int n = 3242;
	decToHexa(n);
	return 0;
}
// C++ program to convert decimal number to hexadecimal
#include <iostream>
using namespace std; 
// function to convert decimal number to hexadecimal
void decToHexa(int n) { cout << hex << n << endl; }
// driver code
int main()
{
	int n = 3242;
	decToHexa(n);
	return 0;
}

OUTPUT:

CAA

Explanation:

In the above program we are using decTohexa function which makes the call with parameter n which is our decimal number.


Related Topics

C++ Identifier

In a program, C++ identifiers relate to the names of variables, functions, arrays, and other user-defined data types that the programmer has developed. They are a prerequisite for learning any...

4 minutes read.

C++ Type Casting

The type casting of variables in the C++ programming language will be covered in this section. The term "type casting" describes how software converts one data type to another. There...

9 minutes read.

Design Patterns in C++

A design pattern offers an all-encompassing, repeatable answer to the typical issues that arise in software design. Usually, the pattern demonstrates the connections and interactions between several classes or objects....

10 minutes read.

Reverse String Word-Wise in C++

What is a reversed String? Reversing the words of a sentence is called reversed string by words. The difference between the reverse a string and the reverse a string word-wise is...

4 minutes read.

C++ Try-Catch

Every useful program will eventually encounter unexpected outcomes. By entering data that are incorrect, users might create mistakes. Sometimes the program's creator didn't consider all of the options or was...

7 minutes read.

C++ program to read string using cin.getline()

C++ program to read string using cin.getline() C++ getline() is a standard library feature for reading a string or a line from an input source. A getline() function gets characters from...

3 minutes read.

C++ Bidirectional Iterators

Iterators : Iterators serve as a link between algorithms and STL containers, allowing the data inside the container to be modified. They let you to iterate through the container, access and...

3 minutes read.

Structured Binding in C++

Structured binding is the new feature of C++ 17. It is used to bind the specified name with an element of the initializer. Structure binding is used to declare multiple...

3 minutes read.

Single level Inheritance

Inheritance is a fundamental element of C++’s Object-Oriented Programming (OOP). It allows a class (called the derived class) to inherit characteristics and attributes from another class (called the base class)....

5 minutes read.

How to Handle Divide by Zero Exception in C++

If you are a programmer or interested in coding then it is obvious that you face some illogical test cases. Suppose, you have written one program that calculates the factorial...

6 minutes read.

Programs to Print Pyramid Patterns in C++

We will explore how to use code to print a variety of patterns utilizing stars (*), numbers (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,.…)  and alphabets...

7 minutes read.

What does Buffer Flush mean in C++

A buffer flush, to explain simple layman's terms, is nothing but the transfer of computer data which is being stored in a rentable temporary memory of your computer running either...

3 minutes read.

C++ Installation

Let's install C++ setup to start programming in C++. C++ setup contains C++ compiler which is required in your system. There are lots of C++ compilers available, you must choose...

1 minute read.

How to declare a 2D array dynamically in C++

In this article, we will learn how to declare the dynamic array in C++. We also learn the initialization of a 2D array using a pointer in C++. Here, we...

3 minutes read.

Armstrong Number Program in C++

Let's first define Armstrong number before writing the C++ program to check whether the number is Armstrong or not. The sum of the cubes of its digits is equal to the...

2 minutes read.

C++ array of Pointers

Array of Pointers: In high-level programming languages like C++, the array's name is its pointer. The name of an array contains an address which is the address of an element. In...

4 minutes read.

Singleton Design Pattern in C++

Singleton is similar to the global variable. Singleton helps to have only one object of its kind and provides only single access. One of the key features of the singleton...

2 minutes read.

Diamond Pattern in C++ using For Loop

For Loop: A for loop is a repetitive control structure that allows you to create a loop for executing a specific number of times. The syntax of for loop: In C++, a for...

5 minutes read.

Implementation of a Falling Matrix in C++

In many Hollywood and Bollywood movies, we might have seen a programmer who will be referred to as a hacker all the time for some random reason which the writer...

3 minutes read.

Method overriding in C++

What is method overriding? Using the same function in derived class as their base class is referred to as function/method overriding in c++. Method overriding is an example of polymorphism. With...

2 minutes read.