# 12+ CPP Hexadecimal to Decimal Converters

This post contains a total of 12+ Hand-Picked CPP Hexadecimal to Decimal Converter Examples. All the Hexadecimal to decimal converters are made using C++ Programming language.

You can use the source codes of these programs for your own personal or educational use with credits to the original owner.

### Related Posts

Click a Code to Copy it.

## 1. By Manene

Made by Manene. A simple C++ Program to convert hexadecimal to decimal. ( Source )

``````#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
int num;

cin >> dec >> num;

cout<<"Decimal number for given hexadecimal number is : ";
cout << num << endl;

return 0;
}``````

## 2. By Aman Parmar

Made by Aman Parmar. ( Source )

``````#include <bits/stdc++.h>
using namespace std;

int ans=0;
int x=1;

int s=n.size();
for(int i=s-1;i>=0;i--){
if(n[i]>='0' && n[i]<='9'){
ans+=x*(n[i]-'0');
}else if(n[i]>='A' && n[i]<='F'){
ans+=x*(n[i]-'A'+10);
}
x*=16;
}
return ans;
}

int main() {
string n;
cin>>n;

return 0;
}``````

## 3. By Vijaya Vignesh

Made by Vijaya Vignesh. ( Source )

``````#include<iostream>
#include<string.h>
using namespace std;

/*Logic:

Hexadecimal values range from 1 to 9 and from A to F (which represent 10 to 15).

To convert hexadecimal to decimal then iterate each element of the hexadecimal number in the reverse direction.

If the elements are numeric then subtract 48 from their ASCII decimal value; if they equal to any alphabet between A to F, then subtract 55 from their ASCII decimal value.

Since hexadecimal numbers are base 16, each element in from right to left have to multiplied in powers of 16( similar to decimal numbers where each element from left to right are multiplied in powers of 10(ones, tens, hundreds, etc))

The sum of the values of each element gives the decimal value of the hexadecimal number.*/

//Function to convert hexadecimal to decimal. Here the argument is a character array.
int hexa_to_deci(char hexaval[]){

int len = strlen(hexaval);

//Initializing base and decimal output values
int base = 1;
int deci_val = 0;

//Iterating for each value in the hexadecimal in reverse
for(int i = len-1; i >= 0; i--){

//If character value is from 0 to 9, then subtract 48 from the corresponding ASCII value and add it to deci_val
if(hexaval[i] >= '0' && hexaval[i] <= '9'){

deci_val += (hexaval[i] - 48)*base;
base = base * 16;  //increasing base value
}

//If character value is from A to F, then subtract 55 from the corresponding ASCII value and add it to deci_val
else if(hexaval[i] >= 'A' && hexaval[i] <= 'F'){

deci_val += (hexaval[i] - 55)*base;
base = base * 16;
}

}

return deci_val;

}

//Calling the function hexa_to_deci
int main(){

char hexanum[] = "11A";

cout<<hexa_to_deci(hexanum);

return 0;
}``````

## 4. By Ankit Sharma

Made by Ankit Sharma. ( Source )

``````#include <bits/stdc++.h>
using namespace std;

int ans = 0;
int x = 1;
int s = n.size();

for(int i = s-1 ; i>=0 ; i--){
if(n[i] >= '0' && n[i] <= '9'){
ans += x*(n[i] - '0');
}else if(n[i] >= 'A' && n[i] <= 'F'){
ans += x*(n[i] - 'A' + 10);
}x*=16;
}return ans;
}

int main() {
string n;
cin>>n;

return 0;
}``````

## 5. By Malek Alsset

Made by Malek Alsset. ( Source )

``````#include<iostream>
#include<string.h>

using namespace std;

// Function to convert hexadecimal to decimal

{

int len = strlen(hexVal);

// Initializing base value to 1, i.e 16^0

int base = 1;

int dec_val = 0;

// Extracting characters as digits from last character

for (int i=len-1; i>=0; i--)

{

// if character lies in '0'-'9', converting

// it to integral 0-9 by subtracting 48 from

// ASCII value.

if (hexVal[i]>='0' && hexVal[i]<='9')

{

dec_val += (hexVal[i] - 48)*base;

// incrementing base by power

base = base * 16;

}

// if character lies in 'A'-'F' , converting

// it to integral 10 - 15 by subtracting 55

// from ASCII value

else if (hexVal[i]>='A' && hexVal[i]<='F')

{

dec_val += (hexVal[i] - 55)*base;

// incrementing base by power

base = base*16;

}

}

return dec_val;
}

int main()
{

char hexNum[] = "1A";

return 0;
} ``````

## 6. By kkayal

Made by kkayal. Simple command line program to convert between decimal and hexadecimal numbers. ( Source )

``````#include <iostream>
#include <string>
#include <sstream>
#include <cstdlib>

using namespace std;

const char* usage = "Usage:\n\n"
"Example 1: `hex 10` returns 0xA\n"
"Example 2: `hex 0xa` returns 10";

string int_to_hex( long i ) {
stringstream stream;
stream << "0x"<< std::uppercase << std::hex << i;
return stream.str();
}

int main(int argc, char const *argv[]) {
if (argc !=2){
cerr << usage << endl;
return -1;
}

if (string(argv[1]) == "-h" || string(argv[1]) == "-?") {
cout << "Usage:" << usage << endl;
return 0;
}

if (argv[1][0] == '0' && argv[1][1] == 'x')	{
char* pEnd;
long int value = strtol (argv[1], &pEnd, 16);
if(*pEnd == 0) {
cout << value << endl;
return 0;
}
else {
cerr << argv[1] << " is not a valid hex number." << endl;
return -1;
}
} else {
char* pEnd;
long int value = strtol (argv[1], &pEnd, 10);
if(*pEnd == 0) {
cout << int_to_hex(value) << endl;
return 0;
}
else {
cerr << argv[1] << " is not a valid integer." << endl;
return -1;
}
}
}``````

``````#include<bits/stdc++.h>
using namespace std;
int ans=0;
int x=1;
int s=n.size();
for(int i=s-1;i>=0;i--){
if(n[i]>= '0' && n[i]<='9'){
ans += x*(n[i]-'0');
}
else if(n[i]>= 'A' && n[i]<='F'){
ans += x*(n[i]-'A'+10);
}
x*=16;
} return ans;
}

int main()
{
string  n;
cin>>n;
}``````

## 8. By Ryanvfung

Made by Ryanvfung. ( Source )

``````// Zywxn's Hexadecimal Converter v0.1 alpha
// Programmer: Ryan Fung aka "Zywxn"
// Date: 2011-06-13
#include <iostream>
#include <string>
using namespace std;
int main() { // preamble
cout << "Zywxn's Hexadecimal Converter v0.1 alpha" << endl;
bool loop = true; // check if program loop should continue
char input;
string hex = "";
int dec = 0, temp = 0;
while (loop) {
cout << endl << "Choose an option:" << endl << "1: Convert Hexadecimal to Decimal" << endl << "2: Convert Decimal to Hexadecimal" << endl << "3: Exit" << endl;
cin >> input;
if (input=='1') { // Hex to Dec
// prepare for processing text
bool MoreLinesToProcess = true;
while (MoreLinesToProcess) {
// process next line
bool MoreCharactersOnCurrentLine = true;
cout << "--------------------------------------------------" << endl << "Input hexadecimal number: ";
while (MoreCharactersOnCurrentLine) {
// prepare to process next character
char CurrentCharacter;
if (cin.get(CurrentCharacter)) {
// process CurrentCharacter on current line
if (CurrentCharacter == '\n') {
// current line has no more characters
MoreCharactersOnCurrentLine = false;
}
else if (
((CurrentCharacter >= '0') && (CurrentCharacter <= '9')) || // Check if its 0-9
((CurrentCharacter >= 'A') && (CurrentCharacter <= 'F')) ) { // Check if its A-F
if ((CurrentCharacter >= '0') && (CurrentCharacter <= '9')) {dec = (dec * 16) + CurrentCharacter - 48;}
if ((CurrentCharacter >= 'A') && (CurrentCharacter <= 'F')) {
if(CurrentCharacter == 'A') {temp = 10;}
if(CurrentCharacter == 'B') {temp = 11;}
if(CurrentCharacter == 'C') {temp = 12;}
if(CurrentCharacter == 'D') {temp = 13;}
if(CurrentCharacter == 'E') {temp = 14;}
if(CurrentCharacter == 'F') {temp = 15;}
dec = (dec * 16) + temp;
}
}
cout << endl << "Decimal Equivalent: " << dec << endl;
}
else { //no more characters
MoreCharactersOnCurrentLine = false;
MoreLinesToProcess = false;
}
}
//finish processing of current line
dec = 0;
}
// finish overall processing
}
if (input=='2') { // Dec to Hex
cout << "This function is not available yet." << endl;
}
if (input=='3') {loop = false;} // Exit
}
return 0;
}``````

## 9. By nv182001

Made by nv182001. ( Source )

``````#include <bits/stdc++.h>
using namespace std;
int binaryToDecimal(int n){
// ip=101 , op=5
// modulo with 10 then will get last digit , multiply it with 2
int ans = 0;
int x = 1;
while (n>0)
{
ans += x *( n % 10);
x *= 2;
n /= 10;
}
return ans;
}
int octalToDecimal(int n){
// modulo with 10 then will get last digit , multiply it with 8
int ans = 0;
int x = 1;
while (n>0)
{
ans += x *(n % 10);
x *= 8;
n /= 10;
}
return ans;
}

//find size of given n and compare with 0 to 9 and A to F
int ans = 0;
int x = 1;
int s = n.size();
for (int i = s-1; i>=0; i--)
{
if (n[i]>='0'&&n[i]<='9')
{
ans += x * (n[i] - '0');
/* code */
}else if(n[i]>='A'&&n[i]<='F')
{
ans += x * (n[i] - 'A' + 10);
}
x *= 16;
/* code */
}
return ans;
}

int decToBin(int n){
//first modulo and divide by 2
//exp ip=5, op=101
int ans = 0;
int x = 1;
while (n>0)
{
ans += (n % 2) * x;
n = n / 2;
x = x * 10;
}
return ans;
}

int decToOct(int n){
//first modulo and divide by 8
//exp input=100 , output=144
int ans = 0;
int x = 1;
while (n>0)
{
ans += (n % 8) * x;
n = n / 8;
x = x * 10;
}
return ans;
}

int main(){
int n;
cin >> n;
cout << decToOct(n) << endl;
return 0;
}

// int main()
// {
//     int n, rem, i=0;
//     char ans[50];
//     cout<<"Enter the Decimal Number: ";
//     cin>>n;
//     while(n!=0)
//     {
//         rem = n%16;
//         if(rem<10)
//             rem = rem+48;
//         else
//             rem = rem+55;
//         ans[i] = rem;
//         i++;
//         n = n/16;
//     }
//     for(i=i-1; i>=0; i--)
//         cout<<ans[i];
//     cout<<endl;
//     return 0;
// }``````

## 10. By Brigapes

Made by Brigapes. ( Source )

``````#include <iostream>
#include <string>
#include <bitset>
#include <cmath>
//#include <cstring>

using namespace std;

// FUNCTIONS

string string_reverse(string input) {

//string output = "";
//reverse(input, output + strlen(input));
int i, j;
string output(input.length(), ' '); // initially set sufficient length
for (i = 0, j = input.length() - 1; i<input.length(); i++, j--)
output[i] = input[j];

return output;
}

string dec_to_bin(long long input) {
//vars
string bin = "";
string i = "";
unsigned long long in = input;

//loop
while (1) {
i+="1";
if (in % 2 == 1) {
bin = bin + "1";
//(in - 1 / 2);

}
else if (in%2==0) {
bin = bin + "0";
}
if (in <= 1) { break; }
in /= 2;
}
//Output je string bin

//binr = string_reverse(bin);

return string_reverse(bin);

}

unsigned long long deljeno(int input) {
unsigned long long iks = 1;

for (int i = 0; i < input; ++i) {
iks = iks * 10;
}
return iks;
}

string letter_for_hex(int input) {
switch (input) {
case 1: {return "1"; }
case 2: {return "2"; }
case 3: {return "3"; }
case 4: {return "4"; }
case 5: {return "5"; }
case 6: {return "6"; }
case 7: {return "7"; }
case 8: {return "8"; }
case 9: {return "9"; }
case 10 :{return "a"; }
case 11: {return "b"; }
case 12: {return "c"; }
case 13: {return "d"; }
case 14: {return "e"; }
case 15: {return "f"; }
}
}

int number_for_hex(char input) {
if (input=='1') {return 1; }
else if (input == '2') { return 2; }
else if (input == '3') { return 3; }
else if (input == '4') { return 4; }
else if (input == '5') { return 5; }
else if (input == '6') { return 6; }
else if (input == '7') { return 7; }
else if (input == '8') { return 8; }
else if (input == '9') { return 9; }
else if (input == 'a') { return 10; }
else if (input == 'b') { return 11; }
else if (input == 'c') { return 12; }
else if (input == 'd') { return 13; }
else if (input == 'e') { return 14; }
else if (input == 'f') { return 15; }
}

unsigned long long bin_to_dec(long long input) {
long long cac2 = input;
long long vel = 0;
//int incr = 1;
long long temp = 0;
//int incr[5000];
long long output = 0;

//    ******************* Perfect lenght (vel)

while (1) {

//cout << originalinput << " in velikost je " << vel << " cac2 pa " << cac2 << endl;
if (cac2 <= 0)
{
//cac2--; cout << "Velikost je " << vel << endl;
break;
}
cac2/=10;
vel++;
}
temp = input;

int temp2 = 0;
for (; temp2 < vel; ++temp2)
{

temp = input / deljeno(temp2);

if (temp%2==1) {

//temp2++;
int iks = pow(2, temp2);
//output++;
output = output + iks;
}
}

/*  ALT
std::bitset<8> bits();
std::cout << bits.to_ulong(input) << std::endl;
return bits.to.ulong(input);
*/

return output;
}

string dec_to_hex(long long input) {
long long inp = input;

string temp = "";
string hex = "";

while (1) {

if (inp %16 < 1) { break; }
else {
hex = hex + letter_for_hex(inp % 16);
inp /= 16;
}

}
return string_reverse(hex);
//return hex;
}

long long hex_to_dec(string input) {
string inp = string_reverse(input);

long long dec = 0;
long long out = 0;
for (unsigned i = 0; i < inp.length(); ++i)
{
dec = number_for_hex(inp.at(i));
out += dec*(pow(16.0, i));
}

return out;

}

/////////////////////////////  MAIN

int main() {

cout << "Welcome to my number converter! Made by Ugreen." << endl;

int vel = 0;
int cac2 = 0;
string bin = "";
string system;
string binr = "";
bool repeat = false;

unsigned long long originalinput;
int dec_output = 0;

while (1) {
if (repeat) { cout << "Error, no command selected, typo? Try again. " << endl; }
repeat = false;
cout << "Conversion commands are: " << '\n' << "dec_to_bin/bin_to_dec in" << " dec_to_hex/hex_to_dec " << endl;

cin >> system;
cin.clear();
std::cin.ignore(256, '\n');
/*
cout << "Vnesi stevilo" << endl;
cin >> originalinput;
*/

// DECIMAL TO BINARY

if (system == "dec_to_bin") {
cout << "Vnesi stevilo" << endl;
cin >> originalinput;

cout << "Integer " << originalinput << " is " << dec_to_bin(originalinput) << " in binary." << endl;
}

else if (system == "dec_to_hex") {
cout << "Vnesi stevilo" << endl;
cin >> originalinput;
cout << dec_to_hex(originalinput) << endl;
}

else if (system == "hex_to_dec") {
string hex;
cout << "Vnesi stevilo" << endl;
cin >> hex;
cout << hex_to_dec(hex) << endl;
}

// BINARY TO DECIMAL

else if (system == "bin_to_dec") {
cout << "Vnesi stevilo" << endl;
cin >> originalinput;
cout << bin_to_dec(originalinput) << endl;
} //Max 19 binarnih stevil

else { repeat = true; }

if (repeat == false) {
cin.clear();
std::cin.ignore(256, '\n');

cout << "Restart? (Y/N) " << endl;
cin >> system;

if (system == "y" || system == "Y") {
cin.clear();
std::cin.ignore(256, '\n');

vel = 0;
cac2 = 0;
system = "";
bin = "";
binr = "";
//inp = 0;
dec_output = 0;
}

else { break; }
}

}

}``````

## 11. By pondps

Made by pondps. ( Source )

``````#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <math.h>
void decimal(char a[])
{
int b[128], i = 0, *p, c = 0;
while (a[i] != '\0')
{
if ( a[i] == 'A' || a[i] == 'a')
{
b[i] = 10;
}
else if (a[i] == 'B' || a[i] == 'b')
{
b[i] = 11;
}
else if (a[i] == 'C' || a[i] == 'c')
{
b[i] = 12;
}
else if (a[i] == 'D' || a[i] == 'd')
{
b[i] = 13;
}
else if (a[i] == 'E' || a[i] == 'e')
{
b[i] = 14;
}
else if (a[i] == 'F' || a[i] == 'f')
{
b[i] = 15;
}
else
{
b[i] = a[i] - 48;
}
i++;
}
i--;
p = b;
for (i; i >= 0; i--)
{
c += (*p * pow(16, i));
p++;
}
printf("Decimal Number : %d", c);
}
int main()
{
char a[128], i = 0;
scanf("%s", a);
while (a[i] != '\0')
{
if ((a[i] > 70 && a[i] < 97) || a[i] > 102)
{
printf("you enter wrong hexadecimal number ");
return 0;
}
i++;
}
decimal(a);
return 0;
}``````

## 12. By Mgalang

Made by Mgalang. ( Source )

``````#include <iostream>
#include <math.h>
using namespace std;

int main(){

int hexadecimal[100], i = 0, decimal = 0;
char num[255];

cout << "~~~~~~~~~~ Hexadecimal Conversion ~~~~~~~~~~" << endl;
cout << "Enter Hexadecimal Number: ";
gets(num);

while (num[i] > '\0'){

if(num[i] == '0'){
} else if(num[i] == '1'){
} else if(num[i] == '2'){
} else if(num[i] == '3'){
} else if(num[i] == '4'){
} else if(num[i] == '5'){
} else if(num[i] == '6'){
} else if(num[i] == '7'){
} else if(num[i] == '8'){
} else if(num[i] == '9'){
} else if(num[i] == 'A' || num[i] == 'a'){
} else if(num[i] == 'B' || num[i] == 'b'){
} else if(num[i] == 'C' || num[i] == 'c'){
} else if(num[i] == 'D' || num[i] == 'd'){
} else if(num[i] == 'E' || num[i] == 'e'){
} else if(num[i] == 'F' || num[i] == 'f'){
} else{
cout << "Wrong Input";
return 0;
}
i++;
}

int q = i-1;
cout << endl << "\t";
for(int k = 0; k < i; k++){
if(k != i-1){
cout << hexadecimal[k] << "*" << "(16^" << q << ") + ";
} else{
cout << hexadecimal[k] << "*" << "(16^" << q << ")";
}
decimal += hexadecimal[k] * pow(16, q);
q--;
}
cout << "\n\n\tDecimal Value = " << decimal;

return 0;

}``````

## 13. By phurich chanprasit

Made by phurich chanprasit. ( Source )

``````#include<iostream>
using namespace std;

void convert(string x, int e, int i, int sum)
{
int y;
while (e >= 0)
{
char* p = &x[e];
int pw = pow(16, i);
if (*p >= 48 && *p <= 57)
{
y = *p - 48;
}
else if (*p >= 65 && *p <= 70)
{
y = *p - 55;
}
else if (*p >= 97 && *p <= 102)
{
y = *p - 87;
}
sum = sum + (y * pw);
i++;
e--;
}
cout << "Decimal = " << sum << endl;
}

int main()
{
START:
string x;
cout << "Enter characters: ";
cin >> x;
int s = x.size();
int e = s - 1;
int i = 0, c = 0;
int sum = 0;

while (c < s)
{
char* p = &x[c];
if (*p < 48 || (*p > 57 && *p < 65) || (*p > 70 && *p < 97) || *p > 102)
{
cout << "Can enter only 0-1 and A-F." << endl;
system("pause");
system("CLS");
goto START;
}
c++;
}

convert(x, e, i, sum);

return 0;
}``````