首页 > 其他 > 详细

Linux C 知识 char型数字转换为int型 int型 转换为Char

时间:2014-02-12 14:30:11      阅读:321      评论:0      收藏:0      [点我收藏+]

前言

在九度oj做acm的时候,经常会遇到了char类型和int类型相互转化的问题,这里进行一下总结。今后,可能会多次更新博客,因为半年做了很多总结,但是都是保存在word文档上了,现在开始慢慢向CSDN博客转移。

 

问题类型

 

 

char型数字转换为int型

转换方法

  1. a[i] - ‘0‘  

参考程序

  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <string.h>  
  4.   
  5. int main()  
  6. {  
  7.     char str[10];  
  8.     int i, len;  
  9.   
  10.     while(scanf("%s", str) != EOF)  
  11.     {  
  12.         for(i = 0, len = strlen(str); i < len; i++)  
  13.         {  
  14.             printf("%d", str[i] - ‘0‘);  
  15.         }  
  16.         printf("\n");  
  17.     }  
  18.   
  19.     return 0;  
  20. }  
 

 

int类型转化为char类型

转换方法

 

  1. a[i] + ‘0‘  

 


参考程序

 

    1. #include <stdio.h>  
    2. #include <stdlib.h>  
    3. #include <string.h>  
    4.   
    5. int main()  
    6. {  
    7.     int number, i;  
    8.     char str[10];  
    9.   
    10.     while(scanf("%d", &number) != EOF)  
    11.     {  
    12.         memset(str, 0, sizeof(str));  
    13.       
    14.         i = 0;  
    15.         while(number)  
    16.         {  
    17.             str[i ++] = number % 10 + ‘0‘;  
    18.             number /= 10;  
    19.         }         
    20.         puts(str);        
    21.     }  
    22.   
    23.     return 0;  
    24. }  

Linux C 知识 char型数字转换为int型 int型 转换为Char

原文:http://www.cnblogs.com/aspirant/p/3545324.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!