0

im writing a simple program that turns a decimal to hex. im only posting the piece of code where i have the problems

string hexnumeral (int d) {
       int h,l;
       string e,y,f;
       if (d < 16)
             return hexdigit (d);
       else {
            f = d / 16;
            h = d % 16;
            f += h;
            y = hexdigit (h);}
                if (h == 0) 
                      return f += "0";
                else 
                      return f;
}

when is the number is under 16 everything is good. but if its 16 or over i get "0"
the hexdigit fuction is basically a list of
if h=1
return 1
if h==2
return 2
etc its quite long so i omitted it

jjust to make this clear im having the problem startign with the first else in the code

2
Contributors
1
Reply
2
Views
8 Years
Discussion Span
Last Post by Ancient Dragon
0

stringstream is the simplest way to convert an int into hex string

#include <iostream>
#include <string>
#include <sstream>
using namespace std;

int main()
{
    int n = 255;
    string s;
    stringstream stream;
    stream << hex << n;
    stream >>  s;
    cout << s << "\n";
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.