// 自己参考并编写的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. 一条update语句到底加了多少锁?带你深入理解底层原理

    迎面走来了你的面试官,身穿格子衫,挺着啤酒肚,发际线严重后移的中年男子. 手拿泡着枸杞的保温杯,胳膊夹着MacBook,MacBook上还贴着公司标语:"我爱加班". 面试开始,直 ...

  2. identityserver4 (ids4)中如何获取refresh_token刷新令牌token 使用offline_access作用域

    ids4默认自带的api接口/api/connect/token 调用这个接口的时候,需要在body里面的 x-www-form-urlencoded模式下写 {     grant_type: &q ...

  3. C#金额数字转换中文繁体

    /// <summary> /// 数字转换中文繁体金钱 /// </summary> /// <param name="Digital">&l ...

  4. day02 IO

    JAVA IO java io可以让我们用标准的读写操作来完成对不同设备的读写数据工作. java将IO按照方向划分为输入与输出,参照点是我们写的程序. 输入:用来读取数据的,是从外界到程序的方向,用 ...

  5. Template -「矩阵 - 行列式」

    #include <cstdio> int Abs(int x) { return x < 0 ? -x : x; } int Max(int x, int y) { return ...

  6. 使用Python3.7配合协同过滤算法(base on user,基于人)构建一套简单的精准推荐系统(个性化推荐)

    原文转载自「刘悦的技术博客」https://v3u.cn/a_id_136 时至2020年,个性化推荐可谓风生水起,Youtube,Netflix,甚至于Pornhub,这些在互联网上叱咤风云的流媒体 ...

  7. React报错之react component changing uncontrolled input

    正文从这开始~ 总览 当input的值被初始化为undefined,但后来又变更为一个不同的值时,会产生"A component is changing an uncontrolled in ...

  8. 程序员的专属浪漫——用3D Engine 5分钟实现烟花绽放效果

    谁说程序员不懂浪漫? 作为程序员,用自己的代码本事手搓一个技术感十足的惊喜,我觉得,这是不亚于车马慢时代手写信的古典主义浪漫. 那么,应该怎样创作出具有自我身份属性的浪漫惊喜呢? 玩法很多,今天给大家 ...

  9. LuoguP2953 [USACO09OPEN]牛的数字游戏Cow Digit Game(博弈论)

    1~9显然,后面平\(A\)过去 #include <iostream> #include <cstdio> #include <cstring> #include ...

  10. LuoguP2575 高手过招(博弈论)

    空格数变吗?不变呀 阶梯博弈阶梯数变吗?不变呀 那这不就阶梯博弈,每行一栋楼,爬完\(mex\)就可以了吗? #include <iostream> #include <cstdio ...