Base conversion problem

Started by
9 comments, last by Dragonsoulj 14 years, 2 months ago
I am not sure what is the problem? Can anyone spot it ? In C++, visual studio 2008 express.

#include <iostream>
#include <sstream>
#include <string>

using namespace std;

//supports hex, dec and octal
//base can only equal decimal(10),octal(8),or hex(16)
template<typename ReturnType, typename InputType>
ReturnType convertBases(const InputType& value,const int base,bool showBase = true)
{
	stringstream stream;
	
	if(showBase) 
		stream << showbase;

	ReturnType data;

	stream << value;

	switch(base)
	{
	case 10: stream << dec;  break; /* dec by default but...*/ 
	case 16: stream << hex;  break;
	case 8:  stream << oct; break;
	default: throw std::exception("Base not supported"); break;
	}

	if(!(stream >> data) ){
		string errMsg = "Conversion failed\n";
		errMsg +=  "Tried to convert \"" + string(typeid(value).name());
		errMsg +=  "\" into \"" + string(typeid(data).name()) + "\"";
		throw std::exception(errMsg.c_str());
	}
	return data; //all is well?
}
int main(){
	const int decValue = 15;
	enum{ HEX = 16, DEC = 10, OCT = 8 };
	string hexValue;
	string octalValue;
	string deciValue;
	try{
		hexValue = convertBases<string>(decValue,HEX);
		octalValue = convertBases<string>(decValue,OCT);
		deciValue = convertBases<string>(decValue,DEC);
	}catch(std::exception& e) { 
		cout << e.what() << endl;
		return -1; 
	}

	cout <<"For " << decValue << " : \n";
	cout << "hexValue = " << hexValue << endl;
	cout << "octalValue = " << octalValue << endl;
	cout << "decValue = " << deciValue << endl;
	cout << endl;
}
Also if someone can show me the syntax for creating a pointer function for std::ios_base::hex || std::ios_base::dec|| std::ios_base::oct, it would be nice? Thanks.
Edge cases will show your design flaws in your code!
Visit my site
Visit my FaceBook
Visit my github
Advertisement
Quote:Original post by Concentrate
I am not sure what is the problem? Can anyone spot it ? In C++, visual studio 2008
express.

What is it doing? What do you expect it to do instead? I.e., give us a hint :)

Quote:Also if someone can show me the syntax for creating a pointer function for
std::ios_base::hex || std::ios_base::dec|| std::ios_base::oct, it would be nice?
Thanks.

Not sure what is meant by "pointer function" here.
Sorry for the bad info. In the example in main, the output for all is just 15.
For example, in the console, it shows that hexValue = "15", when its supposed to
equal "f". Also by pointer function I mean something of this type :
typedef float (*MathFunc)(float);


I just don't know the return type and the parameter type for std::ios_base::dec or hex or oct.
Edge cases will show your design flaws in your code!
Visit my site
Visit my FaceBook
Visit my github
	switch(base)	{// at this point dec hex and oct are undefined variables as far as I can tell//do you mean to output "dec", "hex" and "oct"// or do you mean DEC HEX OCT from your enum data type ?	case 10: stream << dec;  break; /* dec by default but...*/ 	case 16: stream << hex;  break;	case 8:  stream << oct; break;	default: throw std::exception("Base not supported"); break;	}
Quote:Original post by Concentrate
Sorry for the bad info. In the example in main, the output for all is just 15.
For example, in the console, it shows that hexValue = "15", when its supposed to
equal "f".

The dec, hex, oct manipulators only affect integer input/output. Here, you are outputting a int (with no base set), then reading into a std::string - which is reading back in the (decimal) string previously outputted verbatim. No conversion will occur here.

If you want to express a number as a different base in a std::string, you want something like:
template <typename T>string asBaseStr(T val, int base = 10) {    ostringstream stream;    switch(base) {	case 10: stream << dec; break; /* dec by default but...*/ 	case 16: stream << hex; break;	case 8:  stream << oct; break;	default: throw std::exception("Base not supported"); break;    }    stream << val;    return stream.str();}

Quote:Also by pointer function I mean something of this type :
typedef float (*MathFunc)(float);


I just don't know the return type and the parameter type for std::ios_base::dec or hex or oct.

std::ios_base::hex et al. have type std::ios_base::fmtflags - they're values, not functions.
No, dec,hex and oct are static members of std::ios_base; Since I am using using namespace std, I need not for the fully qualified name.
Edge cases will show your design flaws in your code!
Visit my site
Visit my FaceBook
Visit my github
Oh I got it. It was a stupid mistake like always. This part was the problem :
 stream << value;	switch(base)	{	case 10: stream << dec;  break; /* dec by default but...*/ 	case 16: stream << hex;  break;	case 8:  stream << oct; break;	default: throw std::exception("Base not supported"); break;


I am inserting the value before telling the stream to accept the value
in either hex , dec , or octal. By putting "stream << value " after the
end of switch statement. It works. Thanks guys. Oh and thanks for the website as
well. I will look around there from time to time, now that I know its there.

>> they're values, not functions
Good to know, thanks. For some reason I was under the impression of it being
a function.
Edge cases will show your design flaws in your code!
Visit my site
Visit my FaceBook
Visit my github
Just out of curiosity, don't you need a return 0; or something at the end of your main? You have a return -1; in your catch but I don't see one elsewhere.
Quote:Original post by Dragonsoulj
Just out of curiosity, don't you need a return 0; or something at the end of your main? You have a return -1; in your catch but I don't see one elsewhere.

An explicit return value from main is optional in C++. (0 is returned if a value is not given.)
No, main returns 0 implicitly. The return -1, is the exemplify that the program
did not run as intended. Although adding it can't hurt.
Edge cases will show your design flaws in your code!
Visit my site
Visit my FaceBook
Visit my github

This topic is closed to new replies.

Advertisement