Converting int to base16

Any easy way to do this?
std::cout<<std::hex<<YOUR_NORMAL_INT_VARIABLE;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <string>
 
int main()
{
    std::string d = "0123456789ABCDEF";
    
    int num = 43421;
    std::string res;
 
    while(num > 0)
    {
        res = d[num % 16] + res;
        num /= 16;
    }
    
    std::cout << res << std::endl;
}


Really easy and works for all bases.
Last edited on
naraku9333 wrote:

Really easy and works for all bases.

Taking into account the definition of std::string d = "0123456789ABCDEF"; it works for all 2 <= base <=16.
Last edited on
Topic archived. No new replies allowed.