This is my function that is supposed to take a base ten number and return the same number in a base 16(hexidecimal) format
ie. 45 returns 2D
instead I am getting 6850

string inttohex (int n)
{ 
 ostringstream buffer;
 string result;
 int num;

 num=n%16;	
  if(num<10) buffer<<(num+'0');
  else {buffer<< ((num-10)+'A');} 	
  n/=16;
  if (n<10) buffer<<(n+'0');
  else {buffer<< (n-10+'A');}
 
result=buffer.str();
return result;
}

As I mentioned in your other thread:

I suggest you make a compilable program no longer than 25 lines that demonstrates the issue. You should also put output statements at every place possible inside 'inttohex' so you can see exactly where it is going wrong. You should also do the calculation by hand so you can check the values along the way.

Hey, you have to work with us. Even if you do manage to get someone to write your code for you, what about tomorrow and the day after? You need to do things like I just did, and search "how to convert decimal to hexadecimal". Then you get a link like http://www.permadi.com/tutorial/numHexToDec/index.html
that explains how to do it. You didn't ask me to clarify my previous answer, just started another link without letting anyone know by redirecting us.

Here's something that you need to know before you go to the link above:

The reason we use decimal numbers and arithmetic based on decimal numbers is because we have ten fingers. Way back in time, some guy counting sheep would count with his fingers until all of them were sticking out. When that happened, he took a rock and put it in a pile called the "tens" pile, and closed his fists back. Each time a sheep came through the gate, he stuck out a finger for each sheep until all his fingers were out and repeated the same "drill". When his "tens" pile started getting a lot of rocks, he would check with his fingers to see if there were ten rocks in his tens pile. If there were, he took them all out, and put a rock in his "hundreds" pile. That's why we have numbers that start with one, tens, hundreds... OK NOW STAY AWAKE... If we had twelve fingers, the place after the "ones" place would be "sixteens", the next place would be sixteen times sixteen (16 squared, 16^2) or in decimal "two-fifty-sixes". Your first grade teacher would ask "what is 5 plus 6?" and you would say "B".

Now go read that link, and if you don't get it, and you can't get some non-cyber help to understand hex, then come back and ask here.

This is my function that is supposed to take a base ten number and return the same number in a base 16(hexidecimal) format
ie. 45 returns 2D
instead I am getting 6850

string inttohex (int n)
{ 
 ostringstream buffer;
 string result;
 int num;

 num=n%16;	
  if(num<10) buffer<<(num+'0');
  else {buffer<< ((num-10)+'A');} 	
  n/=16;
  if (n<10) buffer<<(n+'0');
  else {buffer<< (n-10+'A');}
 
result=buffer.str();
return result;
}
This article has been dead for over six months. Start a new discussion instead.