C code 字符串与整数的相互转化
2016-06-21 10:09
309 查看
#include<stdio.h> int str_to_int(const char *str,int *num); void int_to_str(char str[],const int strLen,const int num); int main(int argc,char *argv[]) { char str[16]; int ret = 0,n = 0; while(1) { scanf("%s",str); ret = str_to_int(str,&n); if(ret == 0) { printf("%d\n",n); int_to_str(str,16,n); printf("%s\n",str); } else { printf("error\n"); } } return 0; } int str_to_int(const char *str,int *num) { int sign = 1; *num = 0; const char *p = str; while(*p != '\0') { if( *p >= '0' && *p <= '9' ) { *num = *num * 10 + *p - '0'; p++; } else if( *p == '-' && *num == 0) { sign = -1; p++; } else { return -1; } } *num *= sign; return 0; } void int_to_str(char str[],const int strLen,const int num) { int tmp = 0,n = 0,i = 0; if(num < 0) { n = -num; i = 1; } while(n != 0) { tmp = tmp * 10 + n%10; n /= 10; } while(i<strLen && tmp != 0) { str[i++] = tmp % 10 + '0'; tmp /= 10; } }
相关文章推荐
- 亚马逊服务器搭建pptp方法
- 报错AttributeError: 'module' object has no attribute 'HTTPSConnection'
- iOS 支持 IPv6
- 进制转换
- 接口相关笔记
- java 利用spring JavaMailSenderImpl发送邮件,支持普通文本、附件、html、velocity模板
- android-R.id
- redis jedis存储对象简单操作,map list 自定义对象
- android 遍历所有文件夹和子目录搜索文件
- 6月17日 TP表单验证
- Spring中Quartz的最精简的配置
- CSS---关于内层DIV设置margin-top不起作用的解决方案
- iOS-socket
- 嵌入式开发之 入门篇(文件传输、交叉编译)
- canvas 状态的保存和恢复 Saving and restoring state
- 宇航员
- 浅谈HTTP中Get与Post的区别
- zabbix的b编译安装
- 一个基于角色的权限控制系统
- Redhat6.5下Mysql5.6.31主从复制教程