// 自己参考并编写的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);
}

itoa与atoi函数的更多相关文章

  1. C语言itoa()函数和atoi()函数详解(整数转字符C实现)

    1.int/float to string/array: C语言提供了几个标准库函数,可以将任意类型(整型.长整型.浮点型等)的数字转换为字符串,下面列举了各函数的方法及其说明. ● itoa():将 ...

  2. C语言itoa()函数和atoi()函数详解(整数转字符)

    http://c.biancheng.net/cpp/html/792.html C语言提供了几个标准库函数,可以将任意类型(整型.长整型.浮点型等)的数字转换为字符串. 以下是用itoa()函数将整 ...

  3. C语言itoa函数和atoi 函数

    C语言提供了几个标准库函数,可以将任意类型(整型.长整型.浮点型等)的数字转换为字符串.以下是用itoa()函数将整数转 换为字符串的一个例子: # include <stdio.h>  ...

  4. [置顶] C语言itoa()函数和atoi()函数详解(整数转字符C实现)

    头文件:#include <stdlib.h> atoi() 函数用来将字符串转换成整数(int),其原型为: int atoi (const char * str); [函数说明]ato ...

  5. 【转载】C语言itoa()函数和atoi()函数详解(整数转字符C实现)

    本文转自: C语言itoa()函数和atoi()函数详解(整数转字符C实现) 介绍 C语言提供了几个标准库函数,可以将任意类型(整型.长整型.浮点型等)的数字转换为字符串. int/float to ...

  6. C语言itoa()函数和atoi()函数详解(整数转字符C实现)【转载】

    文章转载自https://www.cnblogs.com/bluestorm/p/3168719.html   C语言提供了几个标准库函数,可以将任意类型(整型.长整型.浮点型等)的数字转换为字符串. ...

  7. memset和memcpy函数、atoi函数

    memset void *memset(void *s,int c,size_t n) 总的作用:将已开辟内存空间 s 的首 n 个字节的值设为值 c.如下: // 1.将已开辟内存空间s的首n个字节 ...

  8. atoi函数——将字符串转换为整数

    atoi在一个叫<cstdlib>的库里,可以把字符串直接转换为整数,贼强势. 还有一个atof,就是换成浮点数,实质上是一样的. 例子: #include<cstdlib> ...

  9. atoi()函数

    原型:int  atoi (const  char  *nptr) 用法:#include  <stdlib.h> 功能:将字符串转换成整型数:atoi()会扫描参数nptr字符串,跳过前 ...

随机推荐

  1. Qt数据可视化(散点图、折线图、柱状图、盒须图、饼状图、雷达图)开发实例

    ​  目录 散点图 折线图 柱状图 水平柱状图 水平堆叠图 水平百分比柱状图 盒须图 饼状图 雷达图 Qt散点图.折线图.柱状图.盒须图.饼状图.雷达图开发实例. 在开发过程中我们会使用多各种各样的图 ...

  2. EasyExcel导出添加批注

    直接看代码.根据个人需要做改动 注:POI也可以做批注,文章链接https://www.cnblogs.com/qq1445496485/p/15622664.html /** * 导出(批注) * ...

  3. Unsupported major.minor version 52.0 (unable to load class org.apache.kafka.clients.producer.Produce异常解决方法

    在控制台输入java -version,查看自己的版本是多少,我的查出来是1.8的.随后将服务器上的改为1.8的就可以了.

  4. Idea 编译jsp生成的class文件路径

    找到work\Catalina\localhost\ 然后访问响应的JSP地址才会动态生成到这个路径下面,不访问不会生成,在org\apache\jsp 下面

  5. 记一次react-hooks项目获取图表图片集合并生成pdf的需求

    需求: 获取子组件中所有图片的dom元素并生成图片,再把生成的图片转化为pdf下载 难点 众所周知,react是单向数据流,倡导f(data)⇒ UI的哲学, 并不建议过多直接操作dom,但是生成图片 ...

  6. CentOS 定时计划任务设置

    一.安装crontab服务并设置开机自启 yum install crontabs (centos默认就会带,一般不需要安装) systemctl enable crond (设为开机启动) syst ...

  7. placeholder 设置换行三种方式

    在 html 中编写代码时保留代码换行 <textarea name="" id="" cols="30" rows="10 ...

  8. React报错之Cannot assign to 'current' because it is a read-only property

    正文从这开始~ 总览 当我们用一个null值初始化一个ref,但在其类型中不包括null时,就会发生"Cannot assign to 'current' because it is a r ...

  9. Spring源码 14 IOC refresh方法9

    参考源 https://www.bilibili.com/video/BV1tR4y1F75R?spm_id_from=333.337.search-card.all.click https://ww ...

  10. TS 泛型推断好难啊,看看你能写出来不

    前言 最近做东西都在用ts,有时候写比较复杂的功能,如果不熟悉,类型写起来还是挺麻烦的.有这样一个功能,在这里,我们就不以我们现有的业务来举例了,我们还是已Animal举例,来说明场景.通过一个工厂来 ...