hi!im new in c++ i need help how to make a software that convert any number that the user will enter and convert it in any base that the user will enter. It will convert it and show the result.
Edited
by rythreion: n/a
- 7 Contributors
- forum6 Replies
- 1,651 Views
- 5 Years Discussion Span
- comment Latest Post by NathanOliver
daviddoria 334
What have you tried so far? When you come across a specific problem, we'll try to help you out.
mrnutty 761
If you know how to convert from base 10 to base 2, then you know how to convert
from any base to any other base. So the question remains, do you know how to convert
from base 10 to base 2?
tesuji 135
This is a very common task. Seems to be appearing here every other week. If i recall correctly, about two years ago or so i posted a compact solution to this problem (Besides mine there are lots of solutions given by other people too). So seaching the forum won't be a bad idea ;)
-- tesu
Radical Edward 301
> convert any number that the user will enter and convert it in any base that the user will enter
It is harder than you think. Converting to a base means knowing the alphabet of that base and using any special logic for generating values in that base. Functions like itoa normally limit the supported bases to something like [2..36] so that an there is an intuitive alphabet of decimal digits plus latin letters for the largest base. After 36 casing and special characters enter the mix and it is not as intuitive.
Base32 one popular encoding where special logic is normally used. The itoa style conversion is not good enough to conform to RFC 4648. Base64 is another hugely popular encoding that also has special rules where simple conversions do not work.
Edward would probably support itoa rules for every base from [2..36] except base 32 for a robust program that does conversions. Then base32 and base64 from RFC 4648 would be supported. That seems to offer the maximum usefulness without ignoring existing standards.
The itoa type conversion is pretty simple, if that is all you want to support. Two sticky areas are handling negative numbers and getting the full range of allowed ints. Something like the following works, but it is more complicated than the simplest algorithm because of the those two areas. It is also not as efficient as possible to keep things simple.
#include <iostream>
#include <stdexcept>
#include <string>
using namespace std;
int main() try
{
cout << "Enter a number and base: ";
int num, base;
if (cin >> num >> base) {
if (base < 2 || base > 36)
throw out_of_range("Invalid base for conversion");
// Define an alphabet for the largest supported base
const string digits("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ");
bool is_neg = num < 0;
string result;
for (; num; num /= base) {
// Note: abs() is done on each digit instead of the whole num
// to preserve the full range of int
result.insert(result.begin(), digits[abs(num % base)]);
}
if (is_neg)
result.insert(result.begin(), '-');
cout << result << '\n';
}
else {
cerr << "Invalid input\n";
}
}
catch (const out_of_range& ex)
{
cerr << ex.what() <<'\n';
}
Ed does not feel bad about giving you this code. The algorithm is well known and lots of code can be found with a simple web search, but the quality is questionable. So Ed is showing you the solution that you would have gotten somewhere else anyway except now the quality of the code can be controlled to give you a good example. :)
Edited
by Radical Edward: n/a
You can convert number in any base into any other base using this pseudocode:
function ConvertNumber(number, b, d)
begin
newNumber = ""
while number <> "0"
begin
number = Divide(number, b, d, out remainder)
newDigit = ValueToDigit(remainder)
newNumber = Concatenate(newDigit, newNumber)
end
if newNumber ="" then
newNumber = "0"
end
function Divide(number, base, divisor, out remainder)
begin
remainder = 0
result = ""
for i = 0 to Length(number) - 1
begin
digitValue = DigitToValue(number[i])
remainder = base * remainder + digitValue
newDigitValue = remainder / divisor -- integer division
remainder = remainder mod divisor
if newDigitValue > 0 OR result <> "" then
newDigit = ValueToDigit(newDigitValue)
result = Concatenate(result, newDigit)
end
if result = "" then
result = "0"
return result
end
You can read the whole solution and math behind it here: Converting Number Bases
NathanOliver 429
@Zoran_1 Your code is not C++. This is a question is asking about solving this with C++.