#include <iostream>
#include <fstream>
#include <sstream>
#include <cstdlib>
#include <string>

using namespace std;

int dectoint (string st);
int hextoint (string st);
int bintoint (string st);


string inttobin (int n);
string inttohex (int n);

int main()

{
 string st;
 cout<< "Enter a number in dec,hex,or binary: \n";
 getline(cin,st);

 if (st[0]=='$')
 cout << st<< "=" << hextoint(st)<<", "<< st <<", "<< inttobin(hextoint(st))<<endl;

 /*else if(st[0]=='%')
 cout << st<< "=" << inttohex(bintoint(st))<<", "<< inttohex((bintoint(st))<< ", "<<st<< endl;

 else if(st[0]!='$' && st[0]!='%') 
 cout << st<< "=" << inttohex(st)<< ", "<<inttobin(st)<<endl;
*/
return 0;
}


int dectoint(string st)
{
int n=0;
	for (int i=0; i<st.length(); i++)
	{
	 char ch = st [i];
	 n= n*10+(ch-'0');
	}
return n;
}

int hextoint (string st)
{
int n=0;
	for (int i=1; i<st.length(); i++)
	{
	 char ch= st[i];
	 n= n*16+(ch-'0');
	}
return n;
}

int bintoint (string st)
{
int n=0;
	for (int i=1; i<st.length(); i++)
	{
	 char ch= st[i];
	 n= n*2+(ch-'0');
	}
return n;
}	 

string inttobin (int n)
{
 ostringstream buffer;
 string result=buffer.str();
 int num;
 while (n>0) 
 {
	num=n%2;
	if (n>1)buffer<< num; 
 	else if (n ==1) buffer<<n;
 	n=n/2;
 }
 return result;
}

/*string inttohex (int n)
{ 
 ostringstream buffer;
 string result=buffer.str();
 int num;
 while (n>=0)
 {
	num=n%16;
	buffer<<num='0';
	n=n/16;
 }
 return result;
}*/

When ran I enter $13 and get:
$13=19, $13,

In the blank space after the last comma, it should have SOMETHING at least im hoping for 1101 ????

I'm confused. Your title says "string to int to string", so I was looking for StringToInt() and IntToString() functions, but instead found hex and dec and those things. Can you remove all of the unnecessary code and hardcode the input so we can take look at only relevant material?

I figured I was just calling my functions incorrectly. In reverse order actually when I had a function within a function. I can post the new code which has only one error. The error I am getting is more of a set up issue. I cant figure out how this function is not working (its the only one so I figured I could isolate it and post what it looks like)
I should be able to feed in 45 and get 2D in return
but instead I get 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;
}

Entire code with updates.... still trying to fix "string inttohex(int n)" function.

#include <iostream>
#include <fstream>
#include <sstream>
#include <cstdlib>
#include <string>

using namespace std;

int dectoint (string st);
int hextoint (string st);
int bintoint (string st);


string inttobin (int n);
string inttohex (int n);
string inttodec (int n);
string reverse(string st);

int main()

{
 string st;
 cout<< "Enter a number in dec,hex,or binary: \n";
 getline(cin,st);

 if (st[0]=='$')
 cout << st<< " = " << hextoint(st)<<", "<< st << ", %" << inttobin(hextoint(st))<<endl;

 else if(st[0]=='%')
 cout << st <<" = "<< inttodec(bintoint(st)) << ", &" << inttohex(bintoint(st)) << ", " << st << endl;

 else if(st[0]!='$' && st[0]!='%') 
 cout << st<< " = " <<st<<", $"<< inttohex(dectoint(st))<< ", %"<< inttobin(dectoint(st))<<endl;
return 0;
}


int dectoint(string st)
{
int n=0;
	for (int i=0; i<st.length(); i++)
	{
	 char ch = st [i];
	 n= n*10+(ch-'0');
	}
return n;
}

int hextoint (string st)
{
int n=0;
	for (int i=1; i<st.length(); i++)
	{
	 char ch= st[i];
	 n= n*16+(ch-'0');
	}
return n;
}

int bintoint (string st)
{
int n=0;
	for (int i=1; i<st.length(); i++)
	{
	 char ch= st[i];
	 n= n*2+(ch-'0');
	}
return n;
}	 

string inttobin (int n)
{
 ostringstream buffer;
 string result;
 int num;
 while (n>0) 
 {
	num=n%2;
	buffer<< num; 
	n/=2;
result= buffer.str();

}
 return reverse(result);

}

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;
}

string inttodec (int n)
{
ostringstream buffer;
string result;
buffer<< n;
result=buffer.str();
return result;
}

string reverse(string st)
{
 string result = "";
 for (int i=0; i<st.length();++i)
  {char ch = st[i];
   result=ch+result;
  }
return result;
}

Please keep the discussion on the forum so everyone can help and benefit.

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.

thanks?

One of us is wrong about how well your hex conversion is working. Try "$A2" and see if you get 162... Hexadecimal string conversion will require you to convert characters "0" through "9" to integers 0 through 9, and A through F should be converted to 10 through 15. THEN you can multiply and add. If you start at the least significant digit, multiply it times 16^0, (16 raised to the 0 power) next digit is multiplied by 16^1, next by 16^2 etc.
So: 0xCDB is converted this way, code it how you like: (11 is the value for "B") (13 for "D") (12 for "C")

(11 * 16^0) + (13 * 16^1) + (12 * 16^2)

If you put your factors (11, 13, 12)into an array indexed respectively 0,1,2 then you hopefully see where a for-next loop can do the work for you.

I'll be back to check later. Powers of two make easy work of converting positive numbers to binary.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.