Class or Template? Convert integers into decimal, octal, and hexadecimal.
Ok I started writting this as a class, then realized a template may be better.
What I need to do is take 3 numbers (10, 010, 0x10 for example) decimal, octal, and hexadecimal and then output the number in all 3 formats. If 0x10 was input then I would need to output that number in decimal, octal, and hexadecimal format.
Now I figure a template would be the best way to go since 3 different kinds of numbers will be getting input into the same function. But I am not sure how to handle the conversion of these numbers. I think there is something in the stream manipulation that can handle this, but I would appreciate some help figuring it out.
Also, I am not sure, but I think I would need to return an array from the function that held all three conversions of the number. I guess it would have to be a string array unless 0x10 (for example) can be held in a int array. This is also something I do not know how to do..I can do it in VB but not C++.
Any help is appreciated! :wave:
PHP Code:
template<class T>
string convertInt( const T )
{
//convert numbers to dec, hex and oct values and return all 3
}//end convertInt
Re: Class or Template? Convert integers into decimal, octal, and hexadecimal.
This is not somewhere you should use a template, since the numbers are always converted from strings to integers or the other way around.
In this case the easiest way would be to use the C sprintf/sscang functions, because they support the octal and decimal format:
Code:
#include <cstdio>
#include <string>
std::string numberInThreeFormats(const std::string& str) {
char buffer[255]; // large enough to hold any number three times
int number;
std::sscanf(str.c_str(), "%i", number); // %i = integer in decimal, octal or hexadecimal
std::sprintf(buffer, "%d, 0%o, 0x%x", number, number, number); // %d = decimal, %o = octal, %x = hexadecimal
return buffer;
}
All this is also possible using C++ iostreams for example, but you would have to do more work yourself.
Re: Class or Template? Convert integers into decimal, octal, and hexadecimal.
Yah I have to use Iostream in C++ :(. But thanks for the post.
Re: Class or Template? Convert integers into decimal, octal, and hexadecimal.
So does anyone know how to tell if a number is a decimal, hex or oct using iostream?
I need to input a number and tell if its a decimal, hex or oct and if so return what it is...if it isn't then return an error.
That is easy enough if I can figure out how to tell if the input is any of those or not.
Thanks! :wave:
Re: Class or Template? Convert integers into decimal, octal, and hexadecimal.
You could first read it into a string, and then check if it starts with "0x" or "0".
Re: Class or Template? Convert integers into decimal, octal, and hexadecimal.
cout << oct << number << hex << number << dec << number;
This is much work, how?
Re: Class or Template? Convert integers into decimal, octal, and hexadecimal.
Well you see that only converts integers or doubles to into hex dec and oct.
I need to able to accept a hex dec or oct as input and then convert it to all three.
Since a hex (i.e. 0x10) cant be input as an integer or a double then it has to be a string. and those iostream functions do not work on strings.
Re: Class or Template? Convert integers into decimal, octal, and hexadecimal.
how about doing the base conversion manually?
i=last,int b=1,a=0;
while(i>first)a+=*i--*b*=e1;
while(a)*j--=a%e2,a/=e2;
where first and last points to the first and last element in the char array to be converted from base e1 to base e2 in the char array j.
Re: Class or Template? Convert integers into decimal, octal, and hexadecimal.
Why do something manually that you already have?
Re: Class or Template? Convert integers into decimal, octal, and hexadecimal.
well depends, there are always special cases which can be optimised