atoi

What is atoi equivalent for 64bit integer(uint64_t) in C that works on both Unix and Windows?

不问归期 提交于 2019-12-09 04:13:36
问题 I'm trying to convert 64bit integer string to integer, but I don't know which one to use. 回答1: Use strtoull if you have it or _strtoui64() with visual studio. unsigned long long strtoull(const char *restrict str, char **restrict endptr, int base); /* I am sure MS had a good reason not to name it "strtoull" or * "_strtoull" at least. */ unsigned __int64 _strtoui64( const char *nptr, char **endptr, int base ); 回答2: You've tagged this question c++, so I'm assuming you might be interested in C++

Leetcode-字符串转换整数(atoi)

北慕城南 提交于 2019-12-08 06:28:09
Leetcode-字符串转换整数(atoi) 表示题目要求需要理解一下,好懵- - 大概意思是:输入一个字符串,让你找出其中的第一个连续的数字。如果字符串为空、找到的第一个非空格字符为字母就返回0,且找到的数字不能大于2 32 -1、小于-2 32 方法 感觉这个题算法的成分好少,基本上是if-else加上python字符串的一些方法。用到的方法如下: 1.strip():移除左右两边的空白字符 2.isdigit():判断字符串是否为数字 class Solution: def myAtoi(self, str): """ :type str: str :rtype: int """ res_str = '' new_str = str.strip() #strip()函数:移除左右两边的空白字符 if new_str == "": #判断新生成的字符串是否为空 return 0 if new_str[0] == '-' or new_str[0] == '+' : #如果第一个字符为‘-’或‘+’ res_str += new_str[0] for i in range(1,len(new_str)): if new_str[i].isdigit(): #isdigit()函数判断字符串是否为数字 res_str += new_str[i] else: break if len

What can I assume about the behaviour of atoi() on error?

十年热恋 提交于 2019-12-07 01:50:36
问题 The standard C library function atoi is documented in ISO 9899:2011 as: 7.22.1 Numeric conversion functions 1 The functions atof , atoi , atol , and atoll need not affect the value of the integer expression errno on an error. If the value of the result cannot be represented, the behavior is undefined. ... 7.22.1.2 The atoi , atol , and atoll functions Synopsis #include <stdlib.h> int atoi(const char *nptr); long int atol(const char *nptr); long long int atoll(const char *nptr); Description 2

C - Comparing numeric strings

独自空忆成欢 提交于 2019-12-07 01:01:20
问题 Out of professional curiosity, what is the safest / fastest / most efficient way to compare two fully numeric strings in C? #include <stdio.h> #include <string.h> #include <stdlib.h> int main(void){ char str1[5] = "123"; char str2[5] = "123"; char *ptr; if(atoi(str1) == atoi(str2)) printf("Equal strings"); if(strtol(str1,&ptr,10) == strtol(str2,&ptr,10)) printf("Equal strings"); if(strcmp(str1,str2)==0) printf("Equal strings"); return 0; } 回答1: strcmp () in my opinion, as it does not need any

字符串与其他基本类型的转换——从C到C++11

橙三吉。 提交于 2019-12-06 22:35:19
转自 IBM 编译器中国开发团队的《C++11中的string - atoi/itoa》 在C++11中,由于右值引用的引入,常为人所诟病std::string的性能问题得到了很大的改善。另外一方面,我们也可以看到新语言为std::string类增加了很多新的api。比较引人注意的就是std::string的成员函数stoi系列,以及std::to_string全局函数。这两种API虽然很不起眼,却为C++11的格式化输出(formatted I/O)增加了一种实用的手段。我们可以依序会议一下C,C++98,C++11中我们是如何处理atoi/itoa的问题的: 在C时代,通常我们遇到atoi(字符串到数值转换)的问题的时候我们会使用<stdlib.h>中的atoi函数: int num = atoi(cstr); 这里的cstr通常为char 或者const char 类型的字符串。函数返回的结果则是该字符串所表示的一个十进制的integer。函数的整个效果则等同于<stdlib.h>中的另外一个函数strtol: int num = strtol(cstr, NULL, 10); 相比于atoi,strtol多了最后一个参数"radix"表明函数采用的是几进制(这个进制数可以从2到34,这个数值范围的原因显而易见)。除去strtol会在出错时设置全局的errno外

C++ 中int,char,string,CString类型转换(一)

时光怂恿深爱的人放手 提交于 2019-12-06 09:22:10
1. c++中string到int的转换 1) 在C标准库里面,使用atoi: #include <cstdlib> #include <string> std::string text = "152"; int number = std::atoi( text.c_str() ); if (errno == ERANGE) //可能是std::errno { //number可能由于过大或过小而不能完全存储 } else if (errno == ????) //可能是EINVAL { //不能转换成一个数字 } 2) 在C++标准库里面,使用stringstream:(stringstream 可以用于各种数据类型之间的转换) #include <sstream> #include <string> std::string text = "152"; int number; std::stringstream ss; ss << text;//可以是其他数据类型 ss >> number; //string -> int if (! ss.good()) { //错误发生 } ss << number;// int->string string str = ss.str(); if (! ss.good()) { //错误发生 } 3) 在Boost库里面,使用lexical

PHP算法之字符串转换整数 (atoi)

浪子不回头ぞ 提交于 2019-12-06 05:13:52
请你来实现一个 atoi 函数,使其能将字符串转换成整数。 首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止。 当我们寻找到的第一个非空字符为正或者负号时,则将该符号与之后面尽可能多的连续数字组合起来,作为该整数的正负号;假如第一个非空字符是数字,则直接将其与之后连续的数字字符组合起来,形成整数。 该字符串除了有效的整数部分之后也可能会存在多余的字符,这些字符可以被忽略,它们对于函数不应该造成影响。 注意:假如该字符串中的第一个非空格字符不是一个有效整数字符、字符串为空或字符串仅包含空白字符时,则你的函数不需要进行转换。 在任何情况下,若函数不能进行有效的转换时,请返回 0。 说明: 假设我们的环境只能存储 32 位大小的有符号整数,那么其数值范围为 [−231, 231 − 1]。如果数值超过这个范围,请返回 INT_MAX (231 − 1) 或 INT_MIN (−231) 。 示例 1: 输入: "42" 输出: 42 示例 2: 输入: " -42" 输出: -42 解释: 第一个非空白字符为 '-', 它是一个负号。 我们尽可能将负号与后面所有连续出现的数字组合起来,最后得到 -42 。 示例 3: 输入: "4193 with words" 输出: 4193 解释: 转换截止于数字 '3' ,因为它的下一个字符不为数字。 示例 4: 输入

c atoi() for wide chars on linux?

杀马特。学长 韩版系。学妹 提交于 2019-12-05 10:17:39
Is there a c atoi() equivalent for wide chars on Linux? I can find something for MS (wtoi) but I can find anything in a standard Linux lib. You can use wcstol to convert from wide strings to integer values. It is unusual for a Linux program to use wchar_t type. The reason being that Linux uses utf-8 as the standard encoding. char const* strings are assumed to be utf-8 strings by glibc. Ascii digits and utf-8 digits have the same byte representation, so that atoi() works both on ascii and utf-8 strings. Having said that, look in #include <wchar.t> , it provides wcstol() . 来源: https:/

What can I assume about the behaviour of atoi() on error?

限于喜欢 提交于 2019-12-05 07:24:29
The standard C library function atoi is documented in ISO 9899:2011 as: 7.22.1 Numeric conversion functions 1 The functions atof , atoi , atol , and atoll need not affect the value of the integer expression errno on an error. If the value of the result cannot be represented, the behavior is undefined. ... 7.22.1.2 The atoi , atol , and atoll functions Synopsis #include <stdlib.h> int atoi(const char *nptr); long int atol(const char *nptr); long long int atoll(const char *nptr); Description 2 The atoi , atol , and atoll functions convert the initial portion of the string pointed to by nptr to

[LC] 8. String to Integer (atoi)

被刻印的时光 ゝ 提交于 2019-12-04 17:48:04
Implement atoi which converts a string to an integer. The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value. The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function. If the first sequence of non-whitespace characters in str is not a valid integral number, or