如何从C风格的constchar*数组中提取整数
How to extract integer from C style const char* array
我正试图从C风格的const char*数组中提取一个整数。
到目前为止,我有这个:
int Suite::extractParameter(const char* data)
{
//Example data "s_reg2=96"
const char *ptr = strchr(data, '=');
if(ptr)
{
int index = ptr - data;
//Get substring ie. "96"
//Convert substring to int and return
}
else return -1;
}
但我不知道如何提取子字符串,然后将其转换为整数。
要提取的整数介于0和9999之间。
如果字符串总是在'='
字符的末尾,则可以使用std::atoi
:
const char *ptr = strchr(data, '=');
if(ptr && *(ptr+1)) { // it's not NULL and not the last character
int val = std::atoi(ptr+1);
}
演示。
相关文章:
- 从指向结构数组的指针中提取元素
- 在C++如何从数组中提取成员并返回成员类型的数组?
- 如何从字符数组C++中提取2个整数并将它们存储在2个变量中(初学者)
- C 仅使用指针从多维数组中提取特定列
- 将文本文件数据读入字符数组时提取运算符的歧义
- 如何在 c++ 中提取 2D 数组的列?
- 从数组中提取一对数字
- 从自定义结构数组中提取数据
- 需要根据读取的数据类型将ifstream提取到单独的数组中
- 将 __m128i?布尔数组中每个布尔字节的低位提取到打包的位图
- 无法从结构数组 C++ 中提取数据
- 如何从对象数组中提取属性数组?
- 将字符数组的一部分提取为 char*
- 使用C 从定界线拆分字符串中提取整数数组
- 如何将提取的字符串数据转换为字符数组
- QRegExp提取数组名称和索引
- C++ - 从整数数组中插入和提取字符
- 从数组中提取元音,并在另一个数组中存储ND打印
- 如何在不使用堆栈或数组的情况下从INT中提取每个数字?(C )
- 将元素范围从字符数组提取到字符串中