// 自己参考并编写的itoa与atoi函数

// 支持10进制整形,支持16进制整形,支持负数

// 20220228,修复负数字符字符串会转换成正数的bug
#include <stdio.h>

char buffer[128];

char* itoa(int num, char* str, int radix)
{/*索引表*/
char index[] = "0123456789ABCDEF";
unsigned unum;/*中间变量*/
int i = 0, j, k = 0;
/*确定unum的值*/
if (radix == 10 && num < 0)/*十进制负数*/
{
unum = (unsigned)-num;
str[i++] = '-';
k = 1;
}
else if (radix == 16)
{
str[i++] = '0';
str[i++] = 'X';
k = 2;
unum = (unsigned)num;
}
else unum = (unsigned)num;/*其他情况*/
/*转换*/
do {
str[i++] = index[unum % (unsigned)radix];
unum /= radix;
} while (unum);
str[i] = '\0';
/*逆序*/

for (j = k; j <= (i) / 2; j++)
{
char temp;
temp = str[j];
str[j] = str[i - 1 + k - j];
str[i - 1 + k - j] = temp;
}
return str;
}

int atoi(const char *nptr)
{
int radix = 10;
int c; /* current char */
int total; /* current total */
int sign; /* if '-', then negative, otherwise positive */

c = (int)(unsigned char)*nptr++;
sign = c; /* save sign indication */
if (c == '-' || c == '+')
c = (int)(unsigned char)*nptr++; /* skip sign */
// 判断十六进制,0x开头
if (c == '0'
&& (*nptr == 'x' || *nptr == 'X'))
{
radix = 16;
c = (int)(unsigned char)*nptr++; /* skip sign */
c = (int)(unsigned char)*nptr++; /* skip sign */
}

total = 0;

while (1)
{
if ('0' <= c && c <= '9')
{
c = c - '0' + 0;
}
else if ('a' <= c && c <= 'f')
{
c = c - 'a' + 10;
}
else if ('A' <= c && c <= 'F')
{
c = c - 'A' + 10;
}
else
{
break;
}
total = radix * total + c; /* accumulate digit */
c = (int)(unsigned char)*nptr++; /* get next char */
}

if (sign == '-')
return -total;
else
return total; /* return result, negated if necessary */
}

int main()
{
int num;
num = 0x234567;
printf("start.,%d,,,0x%x,,\r\n", num, num);
itoa(num, buffer, 16);
num = atoi(buffer);
printf("case 1::str=%s,,,num=0x%x,%d\n", buffer, num, num);

itoa(num, buffer, 10);
num = atoi(buffer);
printf("case 2::str=%s,,,num=0x%x,%d,\n", buffer, num,num);

num = -52;
printf("\r\n\r\nstart.,%d,,,0x%x,,\r\n", num, num);

itoa(num, buffer, 16);
num = atoi(buffer);
printf("case 3::str=%s,,,num=0x%x,%d\n", buffer, num,num);

itoa(num, buffer, 10);
num = atoi(buffer);
printf("case 4::str=%s,,,num=0x%x,,%d,\n", buffer, num,num);
}

最新文章

  1. 161012、JAVA读写文件,如何避免中文乱码
  2. Quartz 2D Programming Guide
  3. initWithFrame方法的理解
  4. nyoj---t448(寻找最大数)
  5. Java IO 遇到的错误
  6. 一天一个Java基础——反射
  7. 分布式应用框架Akka快速入门
  8. Mac OSX 安装Python的paramiko模块经验总结
  9. NYOJ541 最强DE 战斗力(第五届省赛试题)
  10. PHP学习之-1.7 注释
  11. linux之cut命令简单用法
  12. Spring Cloud 微服务架构学习笔记与示例
  13. hibernate 映射一对多
  14. MS17-010漏洞检测
  15. Python字符串颜色输出
  16. Linux使用一个定时器实现设置任意数量定时器功能【转】
  17. 机器学习理论基础学习16---高斯网络(GN)
  18. js object对象赋值bug和对象复制clone方法
  19. CentOS7 RPM安装 rabbitmqDownloads on Bintray
  20. SharePoint 2013 App 示例之图片墙

热门文章

  1. 给妹子讲python-S01E01好用的列表
  2. 卸载office密钥
  3. 【cartographer_ros】四: 发布和订阅里程计odom信息
  4. 洛谷P2709 小B的询问 莫队做法
  5. linux 配置集群需要修改的东西
  6. OGC WebGIS 常用服务标准(WMS/WMTS/TMS/WFS)速查
  7. SpringBoot数据库管理 - 用Liquibase对数据库管理和迁移?
  8. CF1703B ICPC Balloons 题解
  9. 基于ABP实现DDD--实体创建和更新
  10. 使用JAVA CompletableFuture实现流水线化的并行处理,深度实践总结