一、关于atoi atol的实现

__BEGIN_NAMESPACE_STD
__extern_inline double
__NTH (atof (__const char *__nptr))
{
return strtod (__nptr, (char **) NULL);
}
__extern_inline int
__NTH (atoi (__const char *__nptr))
{
return (int) strtol (__nptr, (char **) NULL, 10);
}
__extern_inline long int
__NTH (atol (__const char *__nptr))
{
return strtol (__nptr, (char **) NULL, 10);
}
__END_NAMESPACE_STD

From /usr/include/stdlib.h

参考:

atoi: Convert string to integer

二、strtol

Parses the C-string str interpreting its content as an integral number of the specified base, which is returned as a long int value. If endptr is not a null pointer, the function also sets the value of endptr to point to the first character after the number.处理C语言字符串,将其内容作为一个某个特定base/进位制的整数,返回的是一个 long int值。如果参数endptr不是一个空指针,函数也会将enptr的值指向number的第一个字符。

The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes as many characters as possible that are valid following a syntax that depends on the base parameter, and interprets them as a numerical value. Finally, a pointer to the first character following the integer representation in str is stored in the object pointed by endptr.这个函数首先会discard掉开头的(尽可能多的)whitespace/空格,直到第一个 非-空格出现。然后,从这个(非-空格)字符开始,取(尽可能多的、且符合base参数规则的)字符 并将这个字符解释为一个数值。

If the value of base is zero, the syntax expected is similar to that of integer constants, which is formed by a succession of:

An optional sign character (+ or -)
An optional prefix indicating octal or hexadecimal base ("0" or "0x"/"0X" respectively)
A sequence of decimal digits (if no base prefix was specified) or either octal or hexadecimal digits if a specific prefix is present

If the base value is between 2 and 36, the format expected for the integral number is a succession of any of the valid digits and/or letters needed to represent integers of the specified radix (starting from '0' and up to 'z'/'Z' for radix 36). The sequence may optionally be preceded by a sign (either + or -) and, if base is 16, an optional "0x" or "0X" prefix.

If the first sequence of non-whitespace characters in str is not a valid integral number as defined above, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed.

For locales other than the "C" locale, additional subject sequence forms may be accepted.

strtol

关于atoi的实现的更多相关文章

  1. [LeetCode] String to Integer (atoi) 字符串转为整数

    Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...

  2. 编写atoi库函数

    看到很多面试书和博客都提到编写atoi函数,在很多面试中面试官都会要求应聘者当场写出atoi函数的实现代码,但基本很少人能写的完全正确,倒不是这道题有多么高深的算法,有多么复杂的数据结构,只因为这道题 ...

  3. 行程编码(atoi函数)

    #include<iostream> #include<string> #include<vector> using namespace std; void jie ...

  4. No.008:String to Integer (atoi)

    问题: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ca ...

  5. c/c++面试题(8)memcopy/memmove/atoi/itoa

    1.memcpy函数的原型: void* memcpy(void* dest,cosnt void* src,size_t n); 返回值:返回dest; 功能:从源内存地址src拷贝n个字节到des ...

  6. LeetCode 7 -- String to Integer (atoi)

    Implement atoi to convert a string to an integer. 转换很简单,唯一的难点在于需要开率各种输入情况,例如空字符串,含有空格,字母等等. 另外需在写的时候 ...

  7. [LeetCode] 8. String to Integer (atoi)

    Implement atoi to convert a string to an integer. public class Solution { public int myAtoi(String s ...

  8. atoi()函数

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

  9. [Leetcode]String to Integer (atoi) 简易实现方法

    刚看到题就想用数组做,发现大多数解也是用数组做的,突然看到一个清新脱俗的解法: int atoi(const char *str) { ; int n; string s(str); istrings ...

  10. 【leetcode】atoi (hard) ★

    虽然题目中说是easy, 但是我提交了10遍才过,就算hard吧. 主要是很多情况我都没有考虑到.并且有的时候我的规则和答案中的规则不同. 答案的规则: 1.前导空格全部跳过  “      123” ...

随机推荐

  1. MYSQL- 创建和删除临时表

    临时表可能是非常有用的,在某些情况下,保持临时数据.最重要的是应该知道的临时表是,他们将当前的客户(www.111cn.net)端会话终止时被删除 当你创建临时表的时候,你可以使用temporary关 ...

  2. 如何创建一个有System用户权限的命令行

    博客搬到了fresky.github.io - Dawei XU,请各位看官挪步.最新的一篇是:如何创建一个有System用户权限的命令行.

  3. ModSecurity CRS笔记[转]

    转自:http://danqingdani.blog.163.com/blog/static/186094195201472304841643/ ModSecurity的规则因为奇怪的正则(可读性差? ...

  4. java正则表达式入门基础

    一.正则表达式术语 1)元字符 : 非一般字符,具有某种意义的字符.如 : \bX : \b边界符, 以 X开始的单词 2) 常用 : \d : 匹配一个数字 : \d ,  匹配至少一个以上数字 \ ...

  5. UITableview 中获取非选中的cell

    实现效果如图: 在cell中有一个button,选中cell改变button的选择状态 yes,选中另外一个cell,别的cell中的button选择状态变成false. //获取当前可显示的cell ...

  6. 使用ttXactAdmin、ttSQLCmdCacheInfo、ttSQLCmdQueryPlan获取SQL相关具体信息[TimesTen运维]

    使用ttXactAdmin.ttSQLCmdCacheInfo.ttSQLCmdQueryPlan获取SQL相关具体信息,适合于tt11以上版本号. $ ttversion TimesTen Rele ...

  7. Java Singleton 单例模式

    大家可能还听过 Singleton  也就是单例模式 这个单例模式要求 在程序的运行时候   一个程序的某个类 只允许产生一个 实例 那么 这个类就是一个单例类 Java Singleton模式主要作 ...

  8. PHP strlen() 函数

    定义和用法 strlen() 函数返回字符串的长度. 语法 strlen(string) 参数 描述 string 必需.规定要检查的字符串. 例子 <?php echo strlen(&quo ...

  9. delphi 窗体透明

        TransparentColor:=true;    TransparentColorValue:=clFuchsia;    Color:= TransparentColorValue;  ...

  10. Android下pm命令详解

    在看相关PackageManager代码时,无意中发现Android 下提供一个pm命令,通常放在/system/bin/下.这个命令与Package有关,且非常实用.所以研究之. 0. Usage: ...