现在的位置: 首页 > 综合 > 正文

char转int

2013年12月04日 ⁄ 综合 ⁄ 共 685字 ⁄ 字号 评论关闭

#include <stdlib.h>
#include <stdio.h>
int main(void)
{
  int n = 0;
  char *str = "12345.67";
  n = atoi(str);
  printf("string = %s integer = %d\n", str, n);
    
    

    CString strTriNum = _T("123");
    int iTriNum = 0;
    iTriNum = _tstoi(strTriNum);
    cout<<iTriNum<<endl;
  return 0;
}


简单的实现atoi函数源代码


#include <cctype>
int my_atoi(const char* p)
{
  assert(p != NULL);
  bool neg_flag = false;// 符号标记
  int res = 0;// 结果
  if(p[0] == '+' || p[0] == '-')
  neg_flag = (*p++ != '+');
  while(isdigit(*p)) res = res*10 + (*p++ - '0');
  return neg_flag ?0 -res : res;
}

#include <cctype>
int my_atoi(const char* p)
{
  assert(p != NULL);
  bool neg_flag = false;// 符号标记
  int res = 0;// 结果
  if(p[0] == '+' || p[0] == '-')
  neg_flag = (*p++ != '+');
  while(isdigit(*p)) res = res*10 + (*p++ - '0');
  return neg_flag ?0 -res : res;
}

抱歉!评论已关闭.