在C的编程中,接收到的数值有时是char型的数字来显示数值,或者是其不便于进行数据的处理,故本文编了程序来进行转换。
该程序将char型的数字转化为int型,并以一个char来显示(即数值为ascii码)。
复制代码
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
45static int Dec2Str(char * Dec,char * Str,char Dec_Len) { int temp = 0; int temp_len = (int)Dec_Len; if(NULL==Dec || NULL==Str){ printf("the Dec or Str is emptyn"); return -1; } while(temp_len--){ temp = temp*10 + DecChar2Value(*Dec); if(temp<0){ *Str = ''; printf("the high of Dec can not be converted to valuen"); return -2; } Dec++; } if(temp>127){ printf("the input number is too langern"); return -3; } *Str = (char)temp; Str++; *Str = ''; return 0; } static int DecChar2Value(const char DecChar) { int result = 0; if(DecChar>='0' && DecChar<='9'){ result = (int)(DecChar - '0'); } else{ printf("fail to convert DecChar to Valuen"); return -1; } return result; }
该程序只能转换数值低于127的,若想取消该限制,可将程序中的
if(temp>127){
printf("the input number is too langern");
return -3;
}
printf("the input number is too langern");
return -3;
}
注释掉。
最后
以上就是危机背包最近收集整理的关于将char的数值转换为对应的int型数据的全部内容,更多相关将char内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复