After having us write codes to convert decimal numbers to another base my faculty has now asked me to write codes to convert a number from ANY base to another. He explains that itz simple since i already wrote the program to convert integer to another base. So i just need to write the module to convert a number in any base to decimal integer and then just use the function i already have. Well, simple enough. But i was wondering if there's anyway i can do that without first converting the number to decimal and then to another base. Would not that make things faster? or itz just a bad idea since i have to redefine the way C/C++ does itz arithmetic operations?

Straight from one base to another without the intermediate decimal number. So is it possible(i mean easily)?

Recommended Answers

All 5 Replies

After having us write codes to convert decimal numbers to another base my faculty has now asked me to write codes to convert a number from ANY base to another. He explains that itz simple since i already wrote the program to convert integer to another base. So i just need to write the module to convert a number in any base to decimal integer and then just use the function i already have. Well, simple enough. But i was wondering if there's anyway i can do that without first converting the number to decimal and then to another base. Would not that make things faster? or itz just a bad idea since i have to redefine the way C/C++ does itz arithmetic operations?

Straight from one base to another without the intermediate decimal number. So is it possible(i mean easily)?

In order to change the basis you have to do arithmetic. For instance, if you're talking about integers, then the rightmost digit to base N is gotten by dividing the number by N and taking the remaninder. In order to do arithmetic the computer needs a number to work with, rather than a string of digits, which is what you start with. So you have to start by converting the given string to a number. Strictly speaking, you could use octal or hexidecimal, since these are understood by the computer as well as decimal.

"So you have to start by converting the given string to a number."
>>I have to convert the string into a number, and possible base of that number would be hexa, octal or decimal as u say. This is exaclty what i was trying to avoid. Say the user gives a number which is in base 36 and he wants to convert the number to base 31; the way i can do it or the way u r suggesting is that first convert the input into a number that the program understands specifically to octal, hexa or decimal, and then apply arithmetic operation to convert it to it's target base. My question was if i could convert it to base 31 without first converting it to the intermediate octal, decimal or hexadecimal number. So can i? If i can how?

So far i have done it by first converting to int and then to another base. However, now i m having another problem. When i enter 9999999999 in base 10 and convert it to other base it works fine. But when in enter ZZZZZZ in base 36 then everything gets messed up. That's bcos of the overflow of the intermediate decimal number. How do i overcome such overflows?or atleast refrain users from entering such values? I could restrict the number of digits, say to 5. But then the user might want to enter a binary number like 10100101010101001, and my program can work fine with it. Therefore, i believe restricting the number of digit undermines its capability. What should i do?

So far i have done it by first converting to int and then to another base. However, now i m having another problem. When i enter 9999999999 in base 10 and convert it to other base it works fine. But when in enter ZZZZZZ in base 36 then everything gets messed up. That's bcos of the overflow of the intermediate decimal number. How do i overcome such overflows?or atleast refrain users from entering such values? I could restrict the number of digits, say to 5. But then the user might want to enter a binary number like 10100101010101001, and my program can work fine with it. Therefore, i believe restricting the number of digit undermines its capability. What should i do?

Your example of 6Z's in base 36 ought to work if you interpret it as an unsigned long integer. The value of this number is 36^6 -1, which is about 2 billion 176 million. which is well within the range of unsigned long integer.

In general, here's a way to avoid overflow in this conversion: the largest k digit number in base b is b^k -1. To know if you can handle a k digit number in base b use floating point to compute b^k-1. If this is less than the largest value that unsigned long can have, you're ok. Otherwise not.

Good luck.

commented: Thanx.... Asif_NSU +1

In general, here's a way to avoid overflow in this conversion: the largest k digit number in base b is b^k -1. To know if you can handle a k digit number in base b use floating point to compute b^k-1. If this is less than the largest value that unsigned long can have, you're ok. Otherwise not.

This one helped, thanx.

Be a part of the DaniWeb community

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