Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <sstream>
- #include <algorithm>
- using namespace std;
- void hexConverter(int num, string hex)
- {
- if(num!=0)
- {
- int x=num%16;
- num/=16;
- if(x==10)hex+="A";
- else if(x==11)hex+="B";
- else if(x==12)hex+="C";
- else if(x==13)hex+="D";
- else if(x==14)hex+="E";
- else if(x==15)hex+="F";
- else
- {
- stringstream convert;
- convert<<x;
- hex+=convert.str();
- }
- hexConverter(num,hex);
- }
- else
- {
- reverse(hex.begin(),hex.end());
- cout<<hex<<endl;
- }
- }
- int main()
- {
- int num;
- cout<<"Enter a number to convert it to Hexadecimal ";
- cin>>num;
- cout<<num<<" in Hexadecimal is ";
- hexConverter(num,"");
- return 0;
- }
Add Comment
Please, Sign In to add comment