## BASE - Basically Speaking

no tags

The Really Neato Calculator Company, Inc. has recently hired your team to help design their Super Neato Model I calculator. As a computer scientist you suggested to the company that it would be neato if this new calculator could convert among number bases. The company thought this was a stupendous idea and has asked your team to come up with the prototype program for doing base conversion. The project manager of the Super Neato Model I calculator has informed you that the calculator will have the following neato features:

• It will have a 7-digital display.
• Its buttons will include the capital letters A through F in addition to the digits 0 through 9.
• It will support bases 2 through 16.
The input for your prototype program will consist of one base conversion per line. There will be three numbers per line. The first number will be the number in the base you are converting from. The second number is the base you are converting from. The third number is the base you are converting to. There will be one or more blanks surrounding (on either side of) the numbers. There are several lines of input and your program should continue to read until the end of file is reached.

The output will only be the converted number as it would appear on the display of the calculator. The number should be right justified in the 7-digit display. If the number is to large to appear on the display, then print ERROR'' (without the quotes) right justified in the display.

A sample input file is shown here:

```1111000  2 10
1111000  2 16
2102101  3 10
2102101  3 15
12312  4  2
1A 15  2
1234567 10 16
ABCD 16 15
```

The following output file should be produced from the above sample input:

```    120
78
1765
7CA
ERROR
11001
12D687
D071```

## Code Examples

### #1 Code Example with C++ Programming

```Code - C++ Programming```

``````#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <sstream>
#include <map>
#include <list>

using namespace std;

int main(){
string x;
while(cin>>x){
int from,to;
scanf("%d%d",&from,&to);
int base10value=0;
string ans="";
for(int i=x.length()-1;i>=0;i--){
if(x[i]==' ')break;
if((int)(x[i]-'0')>9){
base10value+=((x[i]-'0')-7)*pow(from,x.length()-1-i);
continue;
}
base10value+=(x[i]-'0')*pow(from,x.length()-1-i);
//cout<<base10value<<endl;
}
string temp;
stringstream ss;
ss<<base10value;
temp=ss.str();
if(to==10){
for(int i=temp.length();i<7;i++){
temp=" "+temp;
}
cout<<temp<<endl;
continue;
}
int count=0;
while(base10value<=1){
int tem=(base10value%to);
base10value=base10value/to;
count++;
if(tem>9){
ans=char(tem+55)+ans;
continue;
}
stringstream ss1;
ss1<<tem;
ans=ss1.str()+ans;
}
if(ans.length()>7)ans="ERROR";
for(int i=ans.length();i<7;i++){
ans=" "+ans;
}
cout<<ans<<endl;
}
return 0;
}``````
Copy The Code &

Input

cmd
1111000 2 10
1111000 2 16
2102101 3 10
2102101 3 15
12312 4 2
1A 15 2
1234567 10 16
ABCD 16 15

Output

cmd
120
78
1765
7CA
ERROR
11001
12D687
D071

### #2 Code Example with Java Programming

```Code - Java Programming```

``````/*
*	User: Isanchez_Aguilar
*	Problem: UVA 389 - Basically Speaking
*/
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Scanner;
import java.math.BigInteger;

public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
Scanner in = new Scanner(inputStream);
PrintWriter out = new PrintWriter(outputStream);
UVA389 solver = new UVA389();
solver.solve(1, in, out);
out.close();
}

static class UVA389 {
public void solve(int testNumber, Scanner in, PrintWriter out) {
while (in.hasNext()) {
String numberFrom = in.next();
int baseFrom = in.nextInt();
int baseTo = in.nextInt();
String numberTo = (new BigInteger(numberFrom, baseFrom)).toString(baseTo);

if (numberTo.length() <= 7) {
out.printf("%7s\n", numberTo.toUpperCase());
} else {
out.println("  ERROR");
}
}
}

}
}``````
Copy The Code &

Input

cmd
1111000 2 10
1111000 2 16
2102101 3 10
2102101 3 15
12312 4 2
1A 15 2
1234567 10 16
ABCD 16 15

Output

cmd
120
78
1765
7CA
ERROR
11001
12D687
D071