This post contains a total of 24+ Hand-Picked C++ Decimal to Hexadecimal Converter Examples. All the Decimal to Hexadecimal programs given below 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 Benjamin Ramirez
Made by Benjamin Ramirez. A simple C++ Program to convert decimal number to hexadecimal number. ( Source )
#include <iostream>
using namespace std;
int dec_to_hex(int num){
int select = 0,buffer[255];
if(num == 0){
cout << "Decimal number " << num << " in hexadecimal is " << num << endl;return 0;
}
cout << "Decimal number " << num << " in hexadecimal is ";
while(num > 15){
buffer[select] = num % 16;
num = num / 16;
select ++;
}
if(num < 10){
cout << num;
}
else
{
switch(num){
case 10 : cout << "A" ;break;
case 11 : cout << "B" ;break;
case 12 : cout << "C" ;break;
case 13 : cout << "D" ;break;
case 14 : cout << "E" ;break;
case 15 : cout << "F" ;break;
default : cout << "Switch failed"; return 1; break;
}
}
select --;
while(select >= 0){
if(buffer[select] < 10){
cout << buffer[select];
}
else
{
switch(buffer[select]){
case 10 : cout << "A" ;break;
case 11 : cout << "B" ;break;
case 12 : cout << "C" ;break;
case 13 : cout << "D" ;break;
case 14 : cout << "E" ;break;
case 15 : cout << "F" ;break;
default : cout << "Switch failed"; return 1; break;
}
}
select --;
}
return 0;
}
int main() {
int a;
cin >> a;
dec_to_hex(a);
return 0;
}
2. By nouha coding girl
Made by nouha coding girl. ( Source )
#include <iostream>
using namespace std;
int main()
{
long int decimalNumber,remainder,quotient;
int i=1,j,temp;
char hexadecimalNumber[100];
cout<<"Enter any decimal number: "<<endl;
cin>>decimalNumber;
quotient = decimalNumber;
while(quotient!=0)
{
temp = quotient % 16;
//To convert integer into character
if( temp < 10)
temp =temp + 48;
else
temp = temp + 55;
hexadecimalNumber[i++]= temp;
quotient = quotient / 16;
}
cout<<"nEquivalent hexadecimal number of "<<decimalNumber<<" is : ";
for(j = i -1 ;j> 0;j--)
cout<<hexadecimalNumber[j];
cout<<endl;
return 0;
}
3. By Malek Alsset
Made by Malek Alsset. Change “int n = 282;” to the decimal number you want to use. ( Source )
#include<iostream>
using namespace std;
// function to convert decimal to hexadecimal
void decToHexa(int n)
{
// char array to store hexadecimal number
char hexaDeciNum[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)
{
hexaDeciNum[i] = temp + 48;
i++;
}
else
{
hexaDeciNum[i] = temp + 55;
i++;
}
n = n/16;
}
// printing hexadecimal number array in reverse order
for(int j=i-1; j>=0; j--)
cout << hexaDeciNum[j];
}
int main()
{
int n = 282;
decToHexa(n);
return 0;
}
4. By Jitendra Pal
Made by Jitendra Pal. ( Source )
#include <iostream>
using namespace std;
int main()
{
int a[50],i=1,j,n,t,k;
cout<<"Enter decimal number:"<<endl;
cin>>n;
t=n;
while(n>0)
{
j=n%16;
a[i]=j;
i++;
n=n/16;
}
cout<<"Hexadecimal of "<<t<<" is:";
for(k=i-1;k>=1;k--)
{
cout<<a[k];
}
return 0;
}
5. By Loved by Everyone
Made by Loved by Everyone. ( Source )
#include <iostream>
#include <string>
using namespace std;
string decimaltohexadecimal(int n){
int x=1;
string ans="";
while(x<=n)
x*=16;
x/=16;
while(x>0){
int lastdigit = n/x;
n-=lastdigit*x;
x/=16;
if(lastdigit<=9){
ans= ans + to_string(lastdigit);
} else{
char c= 'A' +lastdigit - 10;
ans.push_back(c);
}
}
return ans;
}
int main() {
int n;
cin>>n;
cout<<decimaltohexadecimal(n)<<endl;
return 0;
}
6. By Kishan Lakhani
Made by Kishan Lakhani. ( Source )
#include<iostream>
#include<iomanip>
using namespace std;
int main ()
{
cout << hex;
cout<<setiosflags(ios::showbase|ios::uppercase);
int n;
cin>>n;
cout<<n;
return 0;
}
7. By Rishu Kumar
Made by Rishu Kumar. Enter the number to be converted in hexadecimal form as input. ( Source )
#include <iostream>
using namespace std;
int main()
{
int x;
cin>>x;
int a[100];
int r; int n=x;
int i=0; int d=0;
while(n!=0)
{
r=n%16;
a[i]=r;
n=n/16;
i++; d++;
}
i--;
int b[100];
int j=0;
while(j<d)
{
b[j]=a[i];
j++;
i--;
}
cout<<"Number entered: "<<x<<endl;
cout<<"Hexadecimal form : ";
i=0;
while(i<d)
{
if(b[i]<10)
cout<<b[i];
else
{ char h[]={"ABCDEF"};
int k=b[i]-10;
cout<<h[k];
}
i++;
}
return 0;
}
8. By ACE
Made by ACE. ( Source )
#include <iostream>
using namespace std;
//convert number to hexadecimal
void HexaDecimal(long long number){
int remainder;
int i=0;
char HexaDecimals[16] = {'0','1','2','3','4',
'5','6','7','8',
'9','A','B','C',
'D','E','F'};
char result[20];
while(number!=0){
remainder = number%16;
result[i] = HexaDecimals[remainder];
number = number/16;
i++;
if(number==0){result[i]='0';}}
bool start = false;
for(int k = 9;k>=0;k--){
if(result[k]=='0')
{start =true;
k=k-1;
}
if(start){
cout<<result[k];}}
}
int main() {
int number;
cin>>number;
HexaDecimal(number);
return 0;
}
9. By Ashwin
Made by Ashwin. ( Source )
#include <iostream>
using namespace std;
void decToHexa(int n)
{
char deciToHexa[100];
int i=0;
int b;
b=n;
while(n!=0)
{
int temp=0;
temp=n%16;
if(temp<10)
{
deciToHexa[i]=temp+48;
i++;
}
else
{
deciToHexa[i]=temp+55;
i++;
}
n=n/16;
}
for(int j=i-1;j>=0;j--)
{
cout<<deciToHexa[j];
}
}
int main() {
int n=2545;
decToHexa(n);
return 0;
}
10. By Kaushal Ranjan
Made by Kaushal Ranjan. ( Source )
#include<iostream>
using namespace std;
int main ()
{
int num, temp, i = 1, j, r;
char hex[50];
cout << " Enter a decimal number : ";
cin >> num;
temp = num;
while (temp != 0)
{
r = temp % 16;
if (r < 10)
hex[i++] = r + 48;
else
hex[i++] = r + 55;
temp = temp / 16;
}
cout << "\nHexadecimal equivalent of " << num << " is : ";
for (j = i; j > 0; j--)
cout << hex[j];
return 0;
}
11. By Mr AJX
Made by Mr AJX . ( Source )
#include <iostream>
using namespace std;
int main()
{
int num;
cout<<"Enter Hexadecimal Number : ";
cin >> hex >> num;
cout<<"Decimal number for given hexadecimal number is : ";
cout << num << endl;
return 0;
}
12. By Mohammad Kaif
Made by Mohammad Kaif. ( Source )
#include <iostream>
#include <string>
using namespace std;
string reverse(string);
string hex(int);
int main()
{
int num = 0;
cout << "Enter a decimal number ";
cin >> num;
cout << endl << "Decimal " << num << " = Hexadecimal " << hex(num);
return 0;
}
string hex(int n)
{
string ans = "";
int rem = 0;
if( n == 0)
{
ans += n + 48;
}
while ( n > 0)
{
rem = n%16;
if(rem >= 0 && rem <= 9)
{
ans += rem + 48;
}
else
{
ans += rem + 55;
}
n /= 16;
}
ans = reverse(ans);
return ans;
}
string reverse(string ans)
{
int len = ans.size();
string mk = ans;
int j = 0;
for(int i = len-1; i >= 0; i--)
{
ans[j] = mk[i];
j++;
}
return ans;
}
13. By Ankit Sharma
Made by Ankit Sharma. A basic C++ program to convert decimal to hexadecimal. ( Source )
#include <bits/stdc++.h>
using namespace std;
//DECIMAL TO HEXADECIMAL
string decimalToHexadecimal (int n){
int x = 1;
string ans ="";
while(x <= n)
x *= 16;
x /= 16;
while(x>0){
int lastdigit = n/x;
n -= lastdigit*x;
x/=16;
if(lastdigit <= 9){
ans = ans+ to_string(lastdigit);}
else {
char c = 'A' + lastdigit - 10;
ans.push_back(c);
}
}return ans;
}
int main() {int n;
cin >> n;
cout<<decimalToHexadecimal(n)<<endl;
return 0;
}
14. By Ankit Kumar
Made by Ankit Kumar. ( Source )
#include <iostream>
using namespace std;
void dec2hex(int n);
int main(){
int n;
cin>>n;
dec2hex(n);
return 0;
}
void dec2hex(int n){
char arr[100];
int i=0;
while(n!=0){
int temp=0;
temp = n%16;
if(temp<10){
arr[i]=temp+48;
i++;
}
else
{
arr[i]=temp+55;
i++;
}
n=n/16;
}
for(int j=i-1;j>=0;j--)
cout<<arr[j];
}
15. By Jake Nelson
Made by Jake Nelson. Enter an integer and it will be converted into a hexadecimal number as a string. ( Source )
#include <iostream>
#include <string>
using namespace std;
//string in dectohex function is assembled in reverse, so it must be reversed to be used
string reverse(string s)
{
string rev = "";
for(int i = s.length(); i >= 0; i--)
{
rev += s[i];
}
return rev;
}
string decToHex(int d)
{
char hex[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
string hexValue = "";
while (d != 0)
{
hexValue += hex[d%16];
d = d / 16;
}
return reverse(hexValue);
}
int main()
{
int input;
cout << "please enter a number" << endl;
cin >> input;
cout << "the number in decimal: " << input << endl;
cout << "the number in hex: " << decToHex(input) << endl;
return 0;
}
16. By ausama.ly
Made by ausama.ly. ( Source )
// Convert Decimal To Hexdecimal.
//---------------------------------
#include <iostream>
using namespace std;
int main()
{
int dec , hex ;
cout << "Enter Number :- ";
cin >> dec ;
while(dec>0)
{
hex = dec % 16;
if (hex==10)
cout << "A" << endl ;
else if (hex==11)
cout << "B" << endl ;
else if (hex==12)
cout << "C" << endl ;
else if (hex==13)
cout << "D" << endl ;
else if (hex==14)
cout << "E" << endl ;
else if (hex==15)
cout << "F" << endl ;
else
cout << hex<< endl ;
dec = dec / 16 ;
}
return 0;
}
17. By Pewien Internauta
Made by Pewien Internauta. ( Source )
#include <iostream>
using namespace std;
int main() {
int n;
cin>>n;
if(n<16777216){
int arr [24];
int a,x=0;
int b=8388608;
cout<<n<<" => ";
while(x<24){
arr [x]=(n-a)/b;
a+=arr[x]*b;
b/=2;
cout<<arr[x];
x++;
}
cout<<" => ";
a=0;
x=0;
int ar[6]{0,0,0,0,0,0};
char ara[6]{'A','B','C','D','E','F'};
b=16;
while (a<6){
b/=2;
ar[a]+=arr[x]*b;
x++;
if(b==1){
b=16;
if(ar[a]>9){
cout<<ara[ar[a]-10];
}
else{
cout<<ar[a];
}
a++;
}
}
}
else{
cout <<"Your number is too big";
}
return 0;
}
18. By Prajwal Yeotkar
Made by Prajwal Yeotkar. ( Source )
#include <iostream>
using namespace std;
int main()
{
int a,n,rem;
cout<<"Enter the decimal number:";
cin>>a;
for(int i=16;a>=i;a/i)
{
rem = a%16;
a = a/16;
cout<<rem;
}
switch(rem)
{
case 1 : cout<<"1";
break;
case 2 : cout<<"2";
break;
case 3 : cout<<"3";
break;
case 4 : cout<<"4";
break;
case 5 : cout<<"5";
break;
case 6 : cout<<"6";
break;
case 7 : cout<<"7";
break;
case 8 : cout<<"8";
break;
case 9 : cout<<"9";
break;
case 10 : cout<<"A";
break;
case 11 : cout<<"B";
break;
case 12 : cout<<"C";
break;
case 13 : cout<<"D";
break;
case 14 : cout<<"E";
break;
case 15 : cout<<"F";
break;
}
return 0;
}
19. By Alex Khorunjiy
Made by Alex Khorunjiy. ( Source )
#include <iostream>
using namespace std;
int main() {
int a,d,aa;
cin >> a;
cout << a << " = ";
if (a > 1048576){
aa =a%1048576;
d=(a-aa)/1048576;
if (d<10)
cout << d;
switch (d){
case 10:
cout << "A";
break;
case 11:
cout << "B";
break;
case 12:
cout << "C";
break;
case 13:
cout << "D";
break;
case 14:
cout << "E";
break;
case 15:
cout << "F";
break;
case 16:
cout << "10";
break;
};
a=aa;
};
if (a > 65536){
aa =a%65536;
d=(a-aa)/65536;
if (d<10)
cout << d;
switch (d){
case 10:
cout << "A";
break;
case 11:
cout << "B";
break;
case 12:
cout << "C";
break;
case 13:
cout << "D";
break;
case 14:
cout << "E";
break;
case 15:
cout << "F";
break;
case 16:
cout << "10";
break;
};
a=aa;
};
if (a > 4096){
aa =a%4096;
d=(a-aa)/4096;
if (d<10)
cout << d;
switch (d){
case 10:
cout << "A";
break;
case 11:
cout << "B";
break;
case 12:
cout << "C";
break;
case 13:
cout << "D";
break;
case 14:
cout << "E";
break;
case 15:
cout << "F";
break;
case 16:
cout << "10";
break;
};
a=aa;
};
if (a > 256){
aa =a%256;
d=(a-aa)/256;
if (d<10)
cout << d;
switch (d){
case 10:
cout << "A";
break;
case 11:
cout << "B";
break;
case 12:
cout << "C";
break;
case 13:
cout << "D";
break;
case 14:
cout << "E";
break;
case 15:
cout << "F";
break;
case 16:
cout << "10";
break;
};
a=aa;
};
if (a > 16){
aa =a%16;
d=(a-aa)/16;
if (d<10)
cout << d;
switch (d){
case 10:
cout << "A";
break;
case 11:
cout << "B";
break;
case 12:
cout << "C";
break;
case 13:
cout << "D";
break;
case 14:
cout << "E";
break;
case 15:
cout << "F";
break;
case 16:
cout << "10";
break;
};
a=aa;
};
if (a >= 0){
aa =a%1;
d=(a-aa)/1;
if (d<10)
cout << d;
switch (d){
case 10:
cout << "A";
break;
case 11:
cout << "B";
break;
case 12:
cout << "C";
break;
case 13:
cout << "D";
break;
case 14:
cout << "E";
break;
case 15:
cout << "F";
break;
case 16:
cout << "10";
break;
};
};
return 0;
}
20. By Damon Teichroeb
Made by Damon Teichroeb. ( Source )
#include <iostream>
using namespace std;
#define ASCII_A 55 // ASCII code for the capital letter A.
#define ASCII_ZERO 48 // ASCII code for the number zero.
#define MAX_HEX 16 // Maximum number of hexadecimal digits.
int main()
{
unsigned long long decimal[MAX_HEX];
short index = 0;
// Gets the user's decimal number.
cin >> decimal[index];
// Converts the decimal number into a hexadecimal number.
while (decimal[index] > 0 && index < MAX_HEX)
{
if (index < MAX_HEX - 1) decimal[index+1] = decimal[index] / 16;
decimal[index] = decimal[index] % 16;
if (decimal[index] > 9) decimal[index] += ASCII_A;
else decimal[index] += ASCII_ZERO;
index++;
}
// Checks to see if the first or last index are reached.
if (index == 0) decimal[index] = ASCII_ZERO;
else if (index == MAX_HEX) index--;
// Prints the hexadecimal number.
cout << "Your hexadecimal number is: ";
for (; index >= 0; index--) cout << (char)decimal[index];
// Says good-bye and terminates the program.
cout << "\n\nThanks for using my program!"
<< "\nHave a nice day! ;-)"
<< "\n\n\n";
return 0;
}
21. By Aman Parmar
Made by Aman Parmar. ( Source )
#include <iostream>
using namespace std;
string decimalToHexadecimal(int n){
int x=1;
string ans="";
while(x<=n){
x*=16;
}
x/=16;
while(x>0){
int lastDigit=n/x;
n-=lastDigit*x;
x/=16;
if(lastDigit<=9){
ans=ans+to_string(lastDigit);
}else{
char c='A'+lastDigit-10;
ans.push_back(c);
}
}
return ans;
}
int main() {
int n;
cin>>n;
cout<<decimalToHexadecimal(n)<<endl;
return 0;
}
22. By mbouchett
Made by mbouchett. Decimal to Hexadecimal using Recursion. ( Source )
/*
* File: main.cpp
* Author: Mark Bouchett
*
* Created on June 30, 2017, 9:26 PM
* This program uses recursion to convert Decimal to Hexadecimal
* decToHex() adapted from Ananthan's Blog(gitHub)
* http://eionix.blogspot.com/2014/12/decimal-to-hex-conversion-using.html
*/
#include <iostream> //console Input/Output
#include <string> //string handler
#include "getInt.h" // fetches an integer
#include <c++/6/iostream>
using namespace std; //declares the name space
// declare function prototypes
void decToHex(int, string &);
int main() {
getInt x; // object ensures an integer
int z = 0; // container to save returned integer
string hex; // holds the hex conversion
//intro
cout << "Decimal to Hexadecimal convertor";
// main program loop
while(z >= 0){
cout << "\n\nEnter an integer for conversion or -1 to quit: ";
hex = ""; //clear the hex container
x.fetchInt(); //get an integer
z = x.returnInt(); //save the integer to z
decToHex(z, hex); //invoke the recursive dec to hex function
if(z >=0 ){ //output unless -1 entered
cout << z << endl;
cout << hex;
}else{ //print output message
cout << "Goodbye!";
}
}
return 0;
}
/**
* converts decimals to hexadecimal using recursion
* @param n //decimal to be converted
* @param hex //reference to hex string
*/
void decToHex(int n, string& hex){
int dhex = 0;
if(!n){
return; //Base case
}else{ //recursive case
dhex = n % 16; //separate the remainder
decToHex(n/16 , hex); //recurse minus the remainder
}
if(dhex > 9) //store the over 9 digits A,B,C,D,E & F
hex += 'A'+(dhex-10); //Ananthan's clever bit to get the over 9 digits
else //store the under 10 digits
hex += to_string(dhex);
}
23. By pondps123
Made by pondps123. ( Source )
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <math.h>
void Hexadecimal(int Num)
{
int num_hexa, i = 0, j = 0;
int a[128], * b;
if (Num > 0)
{
while (Num != 0)
{
num_hexa = Num % 16;
a[i] = num_hexa;
Num /= 16;
i++;
}
b = a;
i--;
printf("Hexadecimal Number : ");
for (i; i >= 0; i--)
{
if (*(b + i) < 10) {
printf("%d", *(b + i));
}
else if (*(b + i) == 10) {
printf("A");
}
else if (*(b + i) == 11) {
printf("B");
}
else if (*(b + i) == 12) {
printf("C");
}
else if (*(b + i) == 13) {
printf("D");
}
else if (*(b + i) == 14) {
printf("E");
}
else if (*(b + i) == 15) {
printf("F");
}
}
}
else if (Num < 0)
{
int NewNum = abs(Num),n = NewNum,j=0;
while (NewNum != 0)
{
num_hexa = NewNum % 16;
a[i] = num_hexa;
NewNum /= 16;
i++;
j++;
}
b = a;
i--;
j--;
printf("Hexadecimal Number (%d) : ", n);
for (i; i >= 0; i--)
{
if (*(b + i) < 10) {
printf("%d", *(b + i));
*(b + i) = abs(*(b + i) - 15);
}
else if (*(b + i) == 10) {
printf("A");
*(b + i) = abs(*(b + i) - 15);
}
else if (*(b + i) == 11) {
printf("B");
*(b + i) = abs(*(b + i) - 15);
}
else if (*(b + i) == 12) {
printf("C");
*(b + i) = abs(*(b + i) - 15);
}
else if (*(b + i) == 13) {
printf("D");
*(b + i) = abs(*(b + i) - 15);
}
else if (*(b + i) == 14) {
printf("E");
*(b + i) = abs(*(b + i) - 15);
}
else if (*(b + i) == 15) {
printf("F");
*(b + i) = abs(*(b + i) - 15);
}
}
printf("\n1's complement = ");
i = j;
for (i; i >= 0; i--)
{
if (*(b + i) < 10) {
printf("%d", *(b + i));
}
else if (*(b + i) == 10) {
printf("A");
}
else if (*(b + i) == 11) {
printf("B");
}
else if (*(b + i) == 12) {
printf("C");
}
else if (*(b + i) == 13) {
printf("D");
}
else if (*(b + i) == 14) {
printf("E");
}
else if (*(b + i) == 15) {
printf("F");
}
}
*b += 1;
for (int k = 0; k <= j; k++)
{
if (*(b + k) == 16)
{
*(b + k) = 0;
*(b + k + 1) += 1;
}
}
printf("\n2's complement = ");
for (j; j >= 0; j--)
{
if (*(b + j) < 10) {
printf("%d", *(b + j));
}
else if (*(b + j) == 10) {
printf("A");
}
else if (*(b + j) == 11) {
printf("B");
}
else if (*(b + j) == 12) {
printf("C");
}
else if (*(b + j) == 13) {
printf("D");
}
else if (*(b + j) == 14) {
printf("E");
}
else if (*(b + j) == 15) {
printf("F");
}
}
}
else if (Num == 0)
{
printf("Hexadecimal Number : 0");
}
}
int main()
{
int a;
printf("Enter Decimal Number : ");
scanf("%d", &a);
Hexadecimal(a);
return 0;
}
24. By IdoloD
Made by IdoloD. ( Source )
#include <stdio.h>
#include <stdlib.h>
//IdoloD github
#define DEBUG 1
//IdoloD github
struct hex_struct {
char* nums;
int top;
int size;
};
//https://github.com/IdoloD/convert-decimal-to-hexadecimal
void reverse(char* nums, int top) {
int i;
for(i=0; i < top/2; i++) {
char swap_var = nums[i];
nums[i] = nums[(top-1)-i];
nums[(top-1)-i] = swap_var;
}
//https://github.com/IdoloD/convert-decimal-to-hexadecimal
}
//IdoloD github
int main () {
int dec_num;
struct hex_struct hex_num;
//IdoloD github
hex_num.top = 0;
hex_num.size = 4;
hex_num.nums = (char*)malloc(hex_num.size*sizeof(char));
//IdoloD github
//https://github.com/IdoloD/convert-decimal-to-hexadecimal
//IdoloD github
do {
printf("Enter a decimal positive number: ");//IdoloD github
scanf("%d", &dec_num);
fflush(stdin);
} while (dec_num < 0);
printf("\n\n");
//IdoloD github
while (dec_num > 0) {
//IdoloD github
if(hex_num.top == hex_num.size) {
hex_num.size *= 2;
//IdoloD github
hex_num.nums = (char*)realloc(hex_num.nums, hex_num.size*sizeof(char));
if(DEBUG)
//IdoloD github
printf("[debug] Reallocated memory from %d byte to %d byte\n", hex_num.size/2, hex_num.size);
}
//IdoloD github
int quoz = dec_num % 16;
dec_num /= 16;
//https://github.com/IdoloD/convert-decimal-to-hexadecimal
char char_quoz;
if (quoz < 10)
//IdoloD github
char_quoz = (char)(48+quoz);
else if(quoz >= 10)
char_quoz = (char)(55+quoz);
//https://github.com/IdoloD/convert-decimal-to-hexadecimal
hex_num.nums[hex_num.top] = char_quoz;
hex_num.top++;
//IdoloD github
}
hex_num.nums[hex_num.top] = '\0';
//https://github.com/IdoloD/convert-decimal-to-hexadecimal
reverse(hex_num.nums, hex_num.top);
printf("the number in hexadecimal is: %s\n", hex_num.nums);
//IdoloD github
printf("Press a button to continue...");
getchar();
return 0;
}
//https://github.com/IdoloD/convert-decimal-to-hexadecimal
25. By Charles Wolfe
Made by Charles Wolfe. A program to convert decimal values to hexadecimal. ( Source )
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <map>
#include <string>
#include <regex>
std::string dec_to_hex(int input) {
std::map<int, std::string> hex;
hex[10] = "A";
hex[11] = "B";
hex[12] = "C";
hex[13] = "D";
hex[14] = "E";
hex[15] = "F";
std::string output = "";
std::vector <int> pow_16;
std::vector <int> place_values;
int remainder;
for (int i = 1; i <= input; i *= 16) {
pow_16.push_back(i);
}
for (int i = pow_16.size() - 1; i >= 0; i--) {
if (input / pow_16[i] == 0) {
place_values.push_back(input / pow_16[i]);
}
else {
place_values.push_back(input / pow_16[i]);
input = input % pow_16[i];
}
}
for (int i = 0; i < place_values.size(); i++) {
if (place_values[i] > 9) {
output += hex[place_values[i]];
}
else {
output += std::to_string(place_values[i]);
}
}
return output;
}
int main()
{
std::regex non_num("[\\D]{1,}");
std::string decimal_str;
int decimal;
bool again = true;
while (again == true) {
std::cout << "Enter a decimal number, or press 'q' to quit: ";
std::cin >> decimal_str;
if (regex_match(decimal_str, non_num) && decimal_str != "q") {
std::cout << "\n";
}
else if (decimal_str == "q"){
std::cout << "\n";
again = false;
}
else {
decimal = std::stoi(decimal_str);
std::cout << "\nHexadecimal equivalent of " << decimal << ": " << dec_to_hex(decimal) << "\n\n";
}
}
return 0;
}