I want to do like this:
cin >> hex >> num;
cout << num ;
cout << dec << num;
cin >> oct >> n;
cout << n;
cin >> base(3) >> nnn;
cout << base(19) << nn;
What I have to do? Have you any code ???
I want to do like this:
cin >> hex >> num;
cout << num ;
cout << dec << num;
cin >> oct >> n;
cout << n;
cin >> base(3) >> nnn;
cout << base(19) << nn;
What I have to do? Have you any code ???
We all are the components of a huge program...... the programmer is always debugging us with His debugger.
You can search the board and probably find some reasonable approaches. When I did this I set up a system whereby I could convert any base to base 10 and then from base 10 to any base, but I'm sure there are different approaches.
I wrote an example program for this, but I don't have it here at work... I can post it this evening (California time).
The decimal, hex, octal input/output is easy using cout. There is a standard function for getting input in (almost) any base [strtoul() = string-to-long]. There is a non-standard function with the Microsoft compiler to display in (almost) any base... I don't remember what it's named.
Hello!
It was working but when the first segment converting hex to dec is not working as it only takes dec form and converts it to oct or hex. any suggestion. ???Code:#include<iostream.h> #include<iomanip.h> #include<stdlib.h> #include<string.h> int main() { char str[20]; unsigned long int n; while(cin.getline(str,20)) { if(str[0]=='0' && str[1]=='x') { char s[20]; for(int i=2;i<strlen(str);++i) s[i-2]=str[i]; n = atol(s); cout << dec << n << endl; } else if(str[0]=='-') { return 0; } else { n = atol(str); cout << "0x"; cout << hex << n << endl; } str[0]=0; n=0; } return 0; }
We all are the components of a huge program...... the programmer is always debugging us with His debugger.
>>n = atol(s);
That won't work. It only works for base 10. You're gonna have to write your own function to go from hex->decimal.
Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah
You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie
Here's a base converter I just threw together quickly. It can convert between bases 2-36.
Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah
You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie
Have fun!
This has a C example, but the strtol() function can be used in C++ too.
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]