safe convert string to integer function
2018-03-07 10:44
399 查看
#include <stdbool.h> #include <stdlib.h> #include <stdint.h> #include <errno.h> #include <assert.h> /* Convert string to integer * * Parses a base-10 number from the given string. Checks that the * string is not blank, contains only numerical characters, and is * within the range of INT32_MIN to INT32_MAX. If the validation is * successful the result is stored in *value; otherwise *value is * unchanged and errno is set appropriately. * * \return true if the number parsed successfully, false on error */ static inline bool safe_strtoint(const char *str, int32_t *value) { long ret; char *end; assert(str != NULL); errno = 0; ret = strtol(str, &end, 10); if (errno != 0) { return false; } else if (end == str || *end != '\0') { errno = EINVAL; return false; } if ((long)((int32_t)ret) != ret) { errno = ERANGE; return false; } *value = (int32_t)ret; return true; }
相关文章推荐
- Write a function to convert an IPv4 Address in string format to an unsigned integer
- Hex string convert to integer with stringstream
- [Ramda] Convert a QueryString to an Object using Function Composition in Ramda
- Implement atoi to convert a string to an integer.
- 《leetCode》:Convert String to Integer
- SQL CONVERT FUNCTION CONVERT DATETIME TO STRING
- Javascript hash functions to convert string into integer hash
- Implement atoi to convert a string to an integer.
- itoa : Convert integer to string
- leetcode 8 Implement atoi to convert a string to an integer.
- Convert integer to string(int类型转化为string类型)
- How Do I Convert an Integer to a String in Excel VBA
- [C++] Hex string convert to integer with stringstream
- 将字符串转换为整数 Convert the string to integer
- Write a program to convert string to number without using library function。
- Journey of Life: sqlite 3 string to integer conversion, similar to C function atoi
- 《LeetBook》leetcode题解(8): String to Integer (atoi) [E]——正负号处理
- Leetcode_StringToInteger
- you *might* want to use the less safe log_bin_trust_function_creators variable
- (atoi)Leetcode第八题_String to Integer (atoi)