Example (12.9) digital conversion, example 12.9 digital conversion
Description |
Returns the conversion of any two non-negative integers of different hexadecimal notation (decimal order ~ The given integer is within the range that long can express. Different hexadecimal notation is ,..., 9, a, B ,..., z) or (0, 1 ,..., 9, A, B ,..., Z ). |
About Input |
The input contains only one row, which contains three integers a, n, and B. A indicates that the n after it is an a-hexadecimal integer, and B indicates that you want to convert a-hexadecimal integer n to a-hexadecimal integer. A and B are decimal integers, 2 ≤ a, B <36. |
About output |
The output contains a row. This row has an integer that is the converted hexadecimal number. Uppercase letters are all used for output, that is, (0, 1,..., 9, A, B,..., Z ). |
Example Input |
15 Aab3 7 |
Example output |
210306 |
Prompt |
Strings can be used to represent Integers of different hexadecimal formats. |
|
# Include <stdio. h> char symbol [256];/* symbol corresponding to the number */int value [256];/* symbol representing the number */void setup () {int j; for (j = 0; j <10; ++ j) {symbol [j] = '0' + j; value ['0' + j] = j ;} for (j = 10; j <36; ++ j) {symbol [j] = 'A' + j-10; value ['A' + j-10] = j; value ['A' + j-10] = j;} long from_base (char * n, int) {long x = 0; while (* n) {x = a * x + value [* n]; n ++;} return x;} char * to_base (long x, char * s, int B) {do {* (-- s) = symbol [x % B]; x = x/B;} while (x); return s ;} int main () {int a = 0, B = 0; char n [256] = {0}; long x = 0; char * p = 0; scanf ("% d % s % d", & a, n, & B); setup (); x = from_base (n, a); p = to_base (x, n + 255, B); printf ("% s \ n", p); return 0 ;} |