最基本的情况,给个10进制数,转化为2进制。。。

 1 #include <stdio.h>
2
3 void Swap(char * a, int i, int j)
4 {
5 char temp = a[i];
6 a[i] = a[j];
7 a[j] = temp;
8 }
9
10 char * Ten2Two(int n)
11 {
12 static char data[100];
13 int i = 0;
14 while (n != 0) {
15 data[i++] = n % 2 + '0';
16 n /= 2;
17 }
18 int j;
19 for (j = 0; j < i / 2 - 1; ++j) {
20 Swap(data, j, i - j - 1);
21 }
22 data[i] = '\0';
23
24 return data;
25 }
26
27 int main()
28 {
29 printf("%s\n", Ten2Two(192));
30
31 return 0;
32 }

 

通用的进制转化问题。见poj 1220

Description

Write a program to convert numbers in one base to numbers in a second base. There are 62 different digits:
{ 0-9,A-Z,a-z }
HINT: If you make a sequence of base conversions using the output of one conversion as the input to the next, when you get back to the original base, you should get the original number.

Input

The first line of input contains a single positive integer. This is the number of lines that follow. Each of the following lines will have a (decimal) input base followed by a (decimal) output base followed by a number expressed in the input base. Both the input base and the output base will be in the range from 2 to 62. That is (in decimal) A = 10, B = 11, ..., Z = 35, a = 36, b = 37, ..., z = 61 (0-9 have their usual meanings).

Output

The output of the program should consist of three lines of output for each base conversion performed. The first line should be the input base in decimal followed by a space then the input number (as given expressed in the input base). The second output line should be the output base followed by a space then the input number (as expressed in the output base). The third output line is blank.

Sample Input

8
62 2 abcdefghiz
10 16 1234567890123456789012345678901234567890
16 35 3A0C92075C0DBF3B8ACBC5F96CE3F0AD2
35 23 333YMHOUE8JPLT7OX6K9FYCQ8A
23 49 946B9AA02MI37E3D3MMJ4G7BL2F05
49 61 1VbDkSIMJL3JjRgAdlUfcaWj
61 5 dl9MDSWqwHjDnToKcsWE1S
5 10 42104444441001414401221302402201233340311104212022133030

Sample Output

62 abcdefghiz
2 11011100000100010111110010010110011111001001100011010010001

10 1234567890123456789012345678901234567890
16 3A0C92075C0DBF3B8ACBC5F96CE3F0AD2

16 3A0C92075C0DBF3B8ACBC5F96CE3F0AD2
35 333YMHOUE8JPLT7OX6K9FYCQ8A

35 333YMHOUE8JPLT7OX6K9FYCQ8A
23 946B9AA02MI37E3D3MMJ4G7BL2F05

23 946B9AA02MI37E3D3MMJ4G7BL2F05
49 1VbDkSIMJL3JjRgAdlUfcaWj

49 1VbDkSIMJL3JjRgAdlUfcaWj
61 dl9MDSWqwHjDnToKcsWE1S

61 dl9MDSWqwHjDnToKcsWE1S
5 42104444441001414401221302402201233340311104212022133030

5 42104444441001414401221302402201233340311104212022133030
10 1234567890123456789012345678901234567890
 1 #include <stdio.h>
2 #include <string.h>
3
4 #define MAX 505
5
6 #define ERROR_CHAR '|'
7
8 void Reverse(char * str)
9 {
10 int l = strlen(str);
11 char temp;
12 int i;
13 for (i = 0; i <= l / 2 - 1; ++i) {
14 temp = str[i];
15 str[i] = str[l - i - 1];
16 str[l - i - 1] = temp;
17 }
18 }
19
20 char Char2Num(char c)
21 {
22 if (c >= '0' && c <= '9')
23 return c - '0';
24 if (c >= 'A' && c <= 'Z')
25 return c - 'A' + 10;
26 if (c >= 'a' && c <= 'z')
27 return c - 'a' + 36;
28
29 printf("error\n");
30 return ERROR_CHAR;
31 }
32
33 char Num2Char(char c)
34 {
35 if (c >= 0 && c <= 9)
36 return c + '0';
37 if (c >= 10 && c <= 35)
38 return c - 10 + 'A';
39 if (c >= 36 && c <= 61)
40 return c - 36 + 'a';
41 printf("error\n");
42 return ERROR_CHAR;
43 }
44 char * BaseTrans(const char * str11, int base1, int base2)
45 {
46 static char str1[MAX];
47 static char str2[MAX];
48 strncpy(str1, str11, MAX);
49 Reverse(str1);
50 int l1 = strlen(str1);
51 int i;
52 for (i = 0; i < l1; ++i) {
53 str1[i] = Char2Num(str1[i]);
54 }
55 int end = l1 - 1;
56 int rest, flag, mod, j = 0;
57 while (1) {
58 flag = false;
59 rest = 0;
60 while (str1[end] == 0 && end >= 0)
61 --end;
62 for (i = end; i >= 0; --i) {
63 mod = (rest * base1 + str1[i] ) % base2;
64 str1[i] = (rest * base1 + str1[i] ) / base2;
65 rest = mod;
66 if (str1[i] != 0) {
67 flag = true;
68 }
69 }
70 str2[j++] = rest;
71 if (!flag) {
72 str2[j] = 0;
73 break;
74 }
75 }
76
77 for (i = j - 1; i >= 0; --i) {
78 str2[i] = Num2Char(str2[i]);
79 }
80 Reverse(str2);
81 return str2;
82 }
83
84 int main()
85 {
86 int case_num, base1, base2;
87 char str[MAX];
88 scanf("%d", &case_num);
89 //printf("%d\n", case_num);
90 while (case_num--) {
91 scanf("%d%d%s", &base1, &base2, str);
92 printf("%d %s\n%d %s\n\n", base1, str, base2, BaseTrans(str, base1, base2));
93 }
94
95 return 0;
96 }

posted on 2011-10-27 18:02  吃吃户  阅读(282)  评论(0编辑  收藏  举报