| | |
hop over decimal and straight to business
![]() |
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)?
Straight from one base to another without the intermediate decimal number. So is it possible(i mean easily)?
•
•
Join Date: Dec 2004
Posts: 60
Reputation:
Solved Threads: 1
•
•
•
•
Originally Posted by Asif_NSU
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)?
"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?
>>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?
•
•
Join Date: Dec 2004
Posts: 60
Reputation:
Solved Threads: 1
•
•
•
•
Originally Posted by Asif_NSU
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?
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.
•
•
•
•
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.
![]() |
Similar Threads
- Cognos BI (Business Intelligence) pros for Canada (Software Development Job Offers)
- Would Love Some Critique (new website) (Website Reviews)
- All You Need for Accessibility - Resources List (Site Layout and Usability)
Other Threads in the C++ Forum
- Previous Thread: 1 bit adder
- Next Thread: Sending email from c++ application
| Thread Tools | Search this Thread |
api array based binary bitmap build c++ c++intmain() c/c++ char class classes client code coding compile console conversion count counttheoccurenceofanintegerinthe10inputs delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption environment error file forms fstream function functions game givemetehcodez graph gui homeworkassignment homeworkhelp homeworkhelper i/o iamthwee ifstream input int integer java jni lib linkedlist linker loop looping loops map math matrix memory multiple multipledimensionarray news node numbers output parameter pointer problem program programming project python radix random read recursion reference rpg string strings temperature template test text text-file tree url variable vector video vtk win32 windows winsock wordfrequency wxwidgets





