954,480 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Base conversions

Any one would help me out to develop a code for a program to convert number from any base to any base..?

doctorkasim
Newbie Poster
10 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
 

Help comes to those who first help themselves. In other words, what have you done so far?

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

according to the logic designed so far by me,i convert numbers from any base to decimal system but failed to convert it further from decimal to the any required base..

doctorkasim
Newbie Poster
10 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
 

Post your code then. Converting from any base to decimal is a good start.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

Can you tell me how i can combine the remainders by repeated division of single digit.For example when 29 is repeatedly divided by 2 then remainders are 1,0,1,1...
I hope you understand the problem

doctorkasim
Newbie Poster
10 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
 

It depends on how you plan to represent the result. This is especially important when converting from base 10 to base 2, because if you're planning on storing the result in an integer type, you can't cover the full range. For example, the largest 32-bit value is ten digits, but the binary representation of that value is 32 digits.

Ideally you would store the represented value as a string and then convert as necessary for calculations. In that case, it's a simple matter of prepending a string with each digit (or appending and reversing at the end):

void dtob(int value, std::string& result)
{
    result.clear();

    while (value != 0) {
        result.insert(result.begin(), (value % 2) + '0');
        value /= 2;
    }
}
Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

Thanks for your kind help..Have a nice time

doctorkasim
Newbie Poster
10 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: