KeeganT

Decimal to Hex Converter

Nov 9th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.82 KB | None | 0 0
  1. #include <iostream>
  2. #include <sstream>
  3. #include <algorithm>
  4.  
  5. using namespace std;
  6.  
  7. void hexConverter(int num, string hex)
  8. {
  9.     if(num!=0)
  10.     {
  11.         int x=num%16;
  12.         num/=16;
  13.         if(x==10)hex+="A";
  14.         else if(x==11)hex+="B";
  15.         else if(x==12)hex+="C";
  16.         else if(x==13)hex+="D";
  17.         else if(x==14)hex+="E";
  18.         else if(x==15)hex+="F";
  19.         else
  20.         {
  21.             stringstream convert;
  22.             convert<<x;
  23.             hex+=convert.str();
  24.         }
  25.         hexConverter(num,hex);
  26.     }
  27.     else
  28.     {
  29.         reverse(hex.begin(),hex.end());
  30.         cout<<hex<<endl;
  31.     }
  32. }
  33.  
  34. int main()
  35. {
  36.     int num;
  37.     cout<<"Enter a number to convert it to Hexadecimal ";
  38.     cin>>num;
  39.     cout<<num<<" in Hexadecimal is ";
  40.     hexConverter(num,"");
  41.     return 0;
  42. }
Add Comment
Please, Sign In to add comment