我正在研究一些读入数据文件的代码.该文件经常包含以ASCII编码的各种长度的数值,我需要将其转换为整数.问题是它们不是以空值终止的,这当然会导致atoi出现问题.我一直在使用的解决方案是手动将null添加到字符序列,然后转换它.
这是我一直在使用的代码; 它工作正常,但它似乎非常kludgy.
char *append_null(const char *chars, const int size)
{
char *tmp = new char[size + 2];
memcpy(tmp, chars, size);
tmp[size + 1] = '\0';
return tmp;
}
int atoi2(const char *chars, const int size)
{
char *tmp = append_null(chars, size);
int result = atoi(tmp);
delete[] tmp;
return result;
}
int main()
{
char *test = new char[20];
test[0] = '1';
test[1] = '2';
test[2] = '3';
test[3] = '4';
cout << atoi2(test, 4) << endl;
}
Run Code Online (Sandbox Code Playgroud)
我想知道是否有更好的方法来解决这个问题.
jth*_*ill 14
固定格式整数转换仍然在库不会执行的手动范围内:
size_t mem_tozd_rjzf(const char *buf, size_t len) // digits only
{
int n=0;
while (len--)
n = n*10 + *buf++ - '0';
return n;
}
Run Code Online (Sandbox Code Playgroud)
long mem_told(const char *buf, size_t len) // spaces, sign, digits
{
long n=0, sign=1;
while ( len && isspace(*buf) )
--len, ++buf;
if ( len ) switch(*buf) {
case '-': sign=-1; \
case '+': --len, ++buf;
}
while ( len-- && isdigit(*buf) )
n = n*10 + *buf++ -'0';
return n*sign;
}
Run Code Online (Sandbox Code Playgroud)
int i = atoi(std::string(chars, size).c_str());
Run Code Online (Sandbox Code Playgroud)