#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[];
int ret = ,n = ;
while()
{
scanf("%s",str);
ret = str_to_int(str,&n);
if(ret == )
{
printf("%d\n",n);
int_to_str(str,,n);
printf("%s\n",str);
}
else
{
printf("error\n");
}
}
return ;
} int str_to_int(const char *str,int *num)
{
int sign = ;
*num = ;
const char *p = str;
while(*p != '\0')
{
if( *p >= '' && *p <= '' )
{
*num = *num * + *p - '';
p++;
}
else if( *p == '-' && *num == )
{
sign = -;
p++;
}
else
{
return -;
}
}
*num *= sign;
return ;
} void int_to_str(char str[],const int strLen,const int num)
{
int tmp = ,n = ,i = ;
if(num < )
{
n = -num;
i = ;
}
while(n != )
{
tmp = tmp * + n%;
n /= ;
}
while(i<strLen && tmp != )
{
str[i++] = tmp % + '';
tmp /= ;
}
}

C code 字符串与整数的相互转化的更多相关文章

  1. leetcode刷题笔记08 字符串转整数 (atoi)

    题目描述 实现 atoi,将字符串转为整数. 在找到第一个非空字符之前,需要移除掉字符串中的空格字符.如果第一个非空字符是正号或负号,选取该符号,并将其与后面尽可能多的连续的数字组合起来,这部分字符即 ...

  2. js计算字符串的字节数和字符串与二进制的相互转化

    一.js计算字符串的字节数方法: //blob获取字符串的字节 var debug = "好的"; var blob = new Blob([debug],{type : 'tex ...

  3. LintCode-54.转换字符串到整数

    转换字符串到整数 实现atoi这个函数,将一个字符串转换为整数.如果没有合法的整数,返回0.如果整数超出了32位整数的范围,返回INT_MAX(2147483647)如果是正整数,或者INT_MIN( ...

  4. ipv4 ipv6 求字符串和整数一一映射的算法 AmazonOrderId

    字符串和整数一一映射的算法 公司每人的英文名不同,现在给每个英文名一个不同的数字编号,怎么设计? 走ipv4/6  2/32 2/128就够了,把“网段”概念对应到“表或库”,ip有a_e5类,这概念 ...

  5. [LeetCode] String to Integer (atoi) 字符串转为整数

    Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...

  6. 【LintCode】转换字符串到整数

    问题描述: 实现atoi这个函数,将一个字符串转换为整数.如果没有合法的整数,返回0.如果整数超出了32位整数的范围,返回INT_MAX(2147483647)如果是正整数,或者INT_MIN(-21 ...

  7. 【剑指offer】字符串转整数

    转载请注明出处:http://blog.csdn.net/ns_code/article/details/28015693 题目描写叙述: 将一个字符串转换成一个整数,要求不能使用字符串转换整数的库函 ...

  8. c++实现atoi()和itoa()函数(字符串和整数转化)

    (0) c++类型所占的字节和表示范围 c 语言里 类型转换那些事儿(补码 反码) 应届生面试准备之道 最值得学习阅读的10个C语言开源项目代码 一:起因 (1)字符串类型转化为整数型(Integer ...

  9. 字符串转换为整数”123“-&gt;123

    字符串转换为整数"123"->123 题目描写叙述: 输入一个由数字组成的字符串.把它转换成整数并输出. 比如:输入字符串"123".输出整数123. 给 ...

随机推荐

  1. c# 模拟 网页实现12306登陆、自动刷票、自动抢票完全篇

    这一篇文章,我将从头到尾教大家使用c#模拟网页面登陆12306网站,自动刷票,选择订票人,到最后一步提交订单.研究过HTTP协议的童鞋们都知道,我们在访问网站时,是有两种方式的,POST和GET方式, ...

  2. 简单几何(极角排序) POJ 2007 Scrambled Polygon

    题目传送门 题意:裸的对原点的极角排序,凸包貌似不行. /************************************************ * Author :Running_Time ...

  3. maven自动化部署插件sshexec-maven-plugin

    在maven pom.xml 文件plugins里增加               <plugin>                 <groupId>com.github.g ...

  4. BZOJ3742 : Painting

    设f[i][j]表示以i为根的子树,i与父亲之间的边染成j的最小代价 DP的过程中转移时相当于求一个最小权匹配,用费用流即可 感觉复杂度飞起来了… #include<cstdio> con ...

  5. HDU 1011 (树形DP+背包)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1011 题目大意:树上取点,先取父亲,再取儿子.每个点,权为w,花费为cost,给定m消费总额,求最大 ...

  6. ios 开发中出现的 pointer being freed was not allocated *** set a breakpoint in malloc_error_break to debug

    主要原因是某部分内存释放的太频繁,解决方法是检查函数的中[xxx release]; 将其注释掉 就行了

  7. 八、job管理

    查看用法: [root@super65 ~]# salt-run -d|grep jobs'jobs.active:' #查看当前执行的job Return a report on all activ ...

  8. Qt 程序退出时断言错误——_BLOCK_TYPE_IS_VALID(pHead->nBlockUse),由setAttribute(Qt::WA_DeleteOnClose)引起

    最近在学习QT,自己仿写了一个简单的QT绘图程序,但是在退出时总是报错,断言错误: 报错主要问题在_BLOCK_TYPE_IS_VALID(pHead->nBlockUse),是在关闭窗口时报的 ...

  9. mac mysql环境配置

    安装mysql:http://www.mysql.com/downloads/ 找到 MySQL Community Edition (GPL) Community (GPL) Downloads » ...

  10. 自动配置IP地址.bat

    ※※※※※※※※※※※※※※※※※※※※※※※※※※※※ @echo     ※                                                    ※ @echo  ...