×

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

Leap Year Program in C++

What is a Leap Year? A solar year is the length of time that it takes for Earth to orbit the Sun - approximately 365.25 days. In a calendar year, we...

4 minutes read.

C++ Multimap

Definition: In C++, a multimap is similar to a map with the additional concept, where multiple elements possess the same keys. It is also not necessary that the key values and...

4 minutes 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.

C++ Writing to file

In file handling, write() function is used to write data into the file. The write() uses ofstream or fstream library to write into the file. Syntax file-stream-class   file-stream-object;   file-stream-object.write((char *)&var , sizeof (var)); The write() takes two arguments. The first argument is the address of variable var...

2 minutes read.

Classes and Objects in C++

When it comes to object-oriented programming, objects are the basic building blocks. Memory is taken up by objects, which contain data and methods or functions that operate on it. On...

3 minutes read.

C++ String Class and its Applications

The String class is available in C++. The character array is represented by the C string. The string class in C++ has a few different attributes. It contains several functions...

4 minutes read.

Armstrong Number using While Loop in C++

What is while Loop? A while loop or while statement repeats all code of its body as long as a specific condition is satisfied. The loop ends if or when the...

4 minutes read.

Hashing in C++

Before understanding hashing, we need to know what the use of hashing is. Let us consider an example of a library which consists of many books.Having many books, searching for...

6 minutes read.

LCM Program in C++

What is LCM? LCM is an acronym for "least common multiple". It is used to discover the lowest positive integer divisible by all integers (whose LCM is calculated). For instance, the...

4 minutes read.

How to create a button in C++

A button is an option through which a user can control to click a provided input to an application. There are several buttons, each with different styles, to maintain the...

3 minutes read.

Templates in C++ vs Generics in Java

As the title suggests, there is no rivalry or there is no cut comparison between generics and templates in Java and C++, respectively. The main aim of this article is...

4 minutes read.

C++ Socket Programming

In this world, computer networking has become very important for sharing of data. Every good programmer has some knowledge about computer networking. Socket programming is one of the critical topics...

6 minutes read.

C++ Forward 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.

Program to find the GCD of two numbers in C++

Before understanding the program of GCD or HCF, one must know what GCD or HCF is. What is GCD? The GCD is referred to as Greatest Common Divisor. HCF is the other...

8 minutes read.

Auto keyword in C++

The primary function of auto keyword is to assign and detect the value of the data type automatically in C++. The compiler knows the data type of the variable by...

2 minutes read.

C++ Range-based For Loop

In C++ language, the range-based for loop was added, which is far superior than the ordinary For loop. The implementation of a range-based for loop doesn't really need much code. It's a...

4 minutes read.

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.

Ways to Copy a Vector in C++

Vectors in C++ are the same as arrays, along with additional outstanding features than them, like array lists in Java programming language. In Vectors, the size constraint is eliminated, which...

5 minutes read.

C++ Continue in for loop

Continue statement: Inside the loop, the continue statement is used to control the loop. C++ utilizes the continue keyword to implement the continue statement, which transfers the program's flow at the start...

4 minutes read.

C++ add two numbers using the function

Here we will learn how to add two numbers by creating function in C++. Let’s learn this with help of example. Code: - #include <iostream> using namespace std; int add_two_no(int a, int b); int main(){   int...

1 minute read.