使用strtol函数进行安全转换
strtol函数比atoi更安全,它允许检测转换是否成功,并能识别非法字符格式。调用时提供字符串和指针用于接收未转换部分,还可以指定进制。例如:long val = strtol(str, &endptr, 10); 若endptr指向字符串末尾,则转换成功。
使用strtol函数进行安全转换
strtol函数比atoi更安全,它允许检测转换是否成功,并能识别非法字符格式。调用时提供字符串和指针用于接收未转换部分,还可以指定进制。例如:long val = strtol(str, &endptr, 10); 若endptr指向字符串末尾,则转换成功。