Not Yet Answered # convert any number to any base..

daviddoria 334 firstPerson 761 tesuji 135 Radical Edward 301 Zoran_1 NathanOliver 429 OK, so HostGator for some reason no longer allows gcc/g++ access unless you have a Designated Server account, which is a lot of money to spend just to compile my "Hello World" program. Thus I figured I'd compile at home, then upload. Program is your regular old bare-bones Hello World ...

0

What have you tried so far? When you come across a specific problem, we'll try to help you out.

1

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?

0

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

0

> 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 6 Years Ago by Radical Edward*: n/a

0

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

0

@Zoran_1 Your code is not C++. This is a question is asking about solving this with C++.

This article has been dead for over six months. Start a new discussion instead.

Recommended Articles

I don’t want at this stage work on a big separate project as I've already got plenty ...

Hi. I have a form with list box : lst_product, datagridview : grd_order and button: btn_addline. lst_product has a list of product ids selected from database (MS Acess 2013) , grd_order is by default empty except for 2 headers and btn_addline adds rows to grd_order.

btn_addline :

`Private Sub btn_addline_Click(ByVal ...`