ASCII值与字符相互转换程序

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <strings.h>
 4 #include <stdlib.h>
 5 /**chartoasc:char -> ascii**/
 6 int chartoasc(char c)
 7 {
 8     int i=0;
 9     i = c;
10     return i;
11 }
12 /**asctochar:ascii -> char**/
13 char asctochar(int a)
14 {
15     char c;
16     c = a;
17     return c;
18 }
19 int main(int argc,char *argv[])
20 {
21 
22     if(argc!=3)
23     {
24         printf("usage:a.out flag char\nflag=1:chartoasc\nflag=2:asctochar\n");
25         return -1;
26     }
27     int flag=0;
28     flag = atoi(argv[1]);
29     char c ;
30     char *p;
31     int a ;
32     if(flag==1)
33     {
34         p = argv[2];
35         c = *p;
36         printf("输入的字符:%c\n",c);
37         a = chartoasc(c);
38         printf("%c的ascii值是:%d\n",c,a);
39     }
40     if(flag==2)
41     {
42         a = atoi(argv[2]);
43         printf("输入的ascii值是:%d\n",a);
44         c = asctochar(a);
45         printf("%d的字符形式是:%c\n",a,c);
46     }
47     return 0;
48 
49 }

 

posted @ 2017-10-26 10:13  子雅陌曦  阅读(740)  评论(0编辑  收藏  举报