I'm new to this site and to C++ in general
I was looking for some way to do the following:

Write a program that allows numbers to be entered in decimal, hexadecimal, or binary, and then prints out the number again in all three bases.

Any help on this program would be fantastic. I appreciate the replies. Thanks much all.

## All 5 Replies

I need to write these three functions to convert strings into integers:

int dectoint( string st ); // e.g. "14" → 14
int hextoint( string st ); // e.g. "14" → 20
int bintoint( string st ); // e.g. "10" → 2

and these three functions to convert integers into strings:

string inttodec( int n ); // e.g. 14 → "14"
string inttohex( int n ); // e.g. 20 → "14"
string inttobin( int n ); // e.g. 2 → "10"

Again, Im confused by this as well.

I need to write these three functions to convert strings into integers:

int dectoint( string st ); // e.g. "14" → 14
int hextoint( string st ); // e.g. "14" → 20
int bintoint( string st ); // e.g. "10" → 2

and these three functions to convert integers into strings:

string inttodec( int n ); // e.g. 14 → "14"
string inttohex( int n ); // e.g. 20 → "14"
string inttobin( int n ); // e.g. 2 → "10"

Again, Im confused by this as well.

Think about what the words hexadecimal, decimal a binary mean.

Binary means base 2, so you never reach 2 and the way numbers are read in binary are--

2^2, 2^(n-1), 2^(n-2)... 2^(0)

--in terms of place-values, much like the decimal system--

10^n, 10^(n-1)... 10^(0)--

--where 10^0 is the ones place. The same applies to hexidecimal except that after 9 you use ABCDEF to express the digit--

G^n, G^(n-1)... G^(0) --

Note that in all of the above cases, for each system, you never reach the number that represents the place value. In Binary, you never reach 2, in Decimal you never reach 10 and in Hexidecimal you never reach G.

Use this to your advantage. For example, the modulus operator give you a remainder of a number based on the divider, for example

23%10, 54%10, 11%10 --

or any number %10 will return a remainder between 0 and 9, and NEVER return a remainder of 10 or any other number outside of that range. You can use the modulus operator to determine the last value of an expression.

You'll also want to use int-division to see how many times a number occurs in a different base system. For example, converting 4 to base 2, you can use 4/2 --

4/2 -- 2 occurs 2 times--

in binary, where is 2 twice expressed?

2^2, 2^1, 2^0 -- its 2^2 therefore the binary expression would be 100, and the remainder to tack on the end--

4%2 = 0

--is zero, so (binary) 100 is the final result.

There's more to say, but I'll leave it to you to try this technique or someone else then present what you can come up with.

hey,

you may try recursive function baseb:

``````void baseb(int b, string fi, unsigned int nb, string &f){
int p = nb % b; nb = nb / b;
if ( nb > 0) baseb(b, fi, nb, f);
f=f+fi[p];
}
int main(){
int b = 16, nb = 2008;
string fi = "0123456789ABCDEF", f = "";
baseb(b, fi, nb, f);
cout << endl;
cout << "Decimal number "<< nb <<" in Base-"<< b << " is "<< f << endl;
// Result: Decimal number 2008 in Base-16 is 7D8
cin.get();
return 0;
}``````

Now it's your turn to program inverse of function baseb.

krs,
tesu

Thank you both for your help, I appreciate it :]

hey,

you may try recursive function baseb:

``````void baseb(int b, string fi, unsigned int nb, string &f){
int p = nb % b; nb = nb / b;
if ( nb > 0) baseb(b, fi, nb, f);
f=f+fi[p];
}
int main(){
int b = 16, nb = 2008;
string fi = "0123456789ABCDEF", f = "";
baseb(b, fi, nb, f);
cout << endl;
cout << "Decimal number "<< nb <<" in Base-"<< b << " is "<< f << endl;
// Result: Decimal number 2008 in Base-16 is 7D8
cin.get();
return 0;
}``````

Now it's your turn to program inverse of function baseb.

krs,
tesu

This code is difficult to understand as your variable names are at most 2 letters long.
Like in this function

``````void baseb(int b, string fi, unsigned int nb, string &f){
int p = nb % b; nb = nb / b;
if ( nb > 0) baseb(b, fi, nb, f);
f=f+fi[p];
}``````

what does variable b, fi, nb, f, and p actually do ?
If your going to name them this way you should at least add some comments.

Be a part of the DaniWeb community

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