atoi 实现 + string转int + int转string

C++代码解析与转换技术
本文详细介绍了C++代码中字符串操作、数值转换、内存处理等关键技巧,并通过实例展示了如何实现字符串到整数的转换、整数到字符串的转换、以及C++代码的高效解析与内存操作。

//j#include <stdio.h>
//#include <stdlib.h>
#include <string>
//#include <ctype.h>//isspace 头文件
#include <iostream>
using namespace std;

long my_atoi(const char *a)
{
    
     while(isspace((int)(unsigned char)*a))
     {
         ++a;                         
     }
     int c;
     c = (int)(unsigned char)*a++;
     int sign = c;
     int total=0;
    
     while(isdigit(c))
     {
          total = total*10 + (c-'0');
          c = (int)(unsigned char)*a++;            
     }
     if(sign=='-')
                  return -total;
     else
         return total;   
}

 

int main()
{
    char *a="123345";  
    int b;
    b= 0;
   
    int num=2349535;
    char n[10];
    itoa(num,n,10);  //int to char*
    printf("%s\n",n);
   
    char m[10];
    sprintf(m,"%d",num);//int to char*
    printf("%s\n",m);
   
    string s=m;
   // cout<<s<<endl;
   printf("%s\n",s.c_str());//string 不是C的内置数据类型,,,,,所以一般用cout.
   
   
    int k=0;
    k=atoi(a);
    printf("%d\n",k);
   
    string aa="       3456    ";
    const char *p=aa.c_str();
    b=my_atoi(p);
    printf("%d\n",b);
 
    char buff[10];
    strcpy(buff,aa.c_str());
    sscanf(buff,"%d",&b);   
    printf("%d\n",b);
       
    system("pause");
    return 0;
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值