-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathP389.cpp
45 lines (43 loc) · 925 Bytes
/
P389.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <stdio.h>
#include <iostream>
int main() {
unsigned long in, from, to;
char out[8];
out[7] = '\0';
while(std::cin >> std::hex >> in >> std::dec >> from >> to) {
unsigned int inAsDec = 0;
unsigned int multiplier = 1;
while(in > 0) {
unsigned int digit = in & 0x0F;
inAsDec += digit*multiplier;
multiplier*=from;
in >>= 4;
}
if(inAsDec == 0) {
printf(" 0\n");
continue;
}
//std::cerr << "In as decimal: " << inAsDec << std::endl;
for(int i = 0; i < 7; ++i)
out[i] = ' ';
int j = 6;
bool done = false;
while(inAsDec > 0) {
if(j < 0) {
printf(" ERROR\n");
done = true;
break;
}
unsigned int digit = inAsDec % to;
if(digit > 9)
out[j--] = 'A' + digit-10;
else
out[j--] = '0' + digit;
inAsDec /= to;
}
if(!done) {
printf("%s\n", out);
}
}
return 0;
}