| C++ Builder 串口控件 | | C++ Builder 编程技巧 | | C++ Builder 操作指南 | | C++ Builder 参考手册 | | 基础知识 | | cfloat 浮点数 | | cmath 数学函数 | | cstdlib 标准库函数 | | • atof, _ttof, _wtof | | • _atold, _ttold, _wtold | | • atoi, _ttoi, _wtoi | | • atol, _ttol, _wtol | | • _atoi64, _ttoi64, _wtoi64 | | • strtod, _tcstod, wcstod | | • strtof, _tcstof, wcstof | | • strtold, _strtold, _tcstold, wcstold, _wcstold | | • strtol, _tcstol, wcstol | | • strtoll, _tcstoll, wcstoll | | • strtoul, _tcstoul, wcstoul | | • strtoull, _tcstoull, wcstoull | | • itoa, _itot, _itow | | • ltoa, _ltoa, _ltot, _ltow | | • ultoa, _ultot, _ultow | | • _i64toa, _i64tot, _i64tow | | • _ui64toa, _ui64tot, _ui64tow | | • ecvt, _ecvt | | • fcvt, _fcvt | | • gcvt, _gcvt | | • abs, labs, llabs | | • div, ldiv, lldiv | | • div_t, ldiv_t, lldiv_t | | • _rotl, _crotl, _lrotl | | • _rotr, _crotr, _lrotr | | • rand, _lrand | | • srand | | • random | | • randomize | | • mbstowcs | | • mblen | | • mbtowc | | • mbtowc_cp | | • wcstombs | | • wctomb | | • wctomb_cp | | • swab, _swab | | • _fullpath, _tfullpath, _wfullpath | | • _makepath, _tmakepath, _wmakepath | | • _splitpath, _tsplitpath, _wsplitpath | | • getenv, _tgetenv, _wgetenv | | • putenv, _putenv, _tputenv, _wputenv | | • _searchenv, _tsearchenv, _wsearchenv | | • _searchstr, _tsearchstr, _wsearchstr | | • system, _tsystem, _wsystem | | • abort | | • atexit | | • atexit_t | | • exit, _exit | | • abort_handler_s | | • ignore_handler_s | | • constraint_handler_t | | • set_constraint_handler_s | | • _argc | | • _argv, _targv, _wargv; | | • _environ, _tenviron, _wenviron | | • EXIT_SUCCESS, EXIT_FAILURE | | • _MAX_PATH, _MAX_DRIVE, _MAX_DIR, _MAX_FNAME, _MAX_EXT | | • MB_CUR_MAX | | • RAND_MAX, LRAND_MAX | | System 字符串 | | System 日期和时间 | | System.Math.hpp 数学函数 | | 其他数据类型 | | VCL 基础类 | | VCL 应用程序 | | Pictures 图片 | | Graphics 绘图 | | Additional 控件 | | System 控件 | | A ~ Z 字母顺序排列的目录 | | 网友留言/技术支持 |
|
| atol, _ttol, _wtol - 字符串转 32 位长整数 (long) |
atol, _ttol, _wtol:字符串转 32 位长整数 (long)
函数原型:
| long atol(const char *s); |
| long _wtol(const wchar_t *s); |
头文件:
#include <cstdlib>
命名空间:
std
参数:
s:整数,格式:"[空白字符][符号][数字]"
返回值:
长整数,long 类型
如果参数 s 包含不可识别的字符,会终止于第一个不可识别的字符,返回前面可识别部分转为整数的值,如果第一个字符不可识别返回值为 0;
如果超出了整数范围会溢出,得到错误的数值 (溢出之后的数值一般会等于丢掉超范围的高位,保留范围之内的低位的数值);
这些错误都不产生异常。
| 字符串 |
转换结果 |
说明 |
| " 12345" |
12345 |
|
| "-54321" |
-54321 |
|
| "2000000000" |
2000000000 |
|
| "3000000000" |
-1294967296 |
3000000000 超出了 long 长整数范围:-2147483648 ~ 2147483647,得到错误的结果 |
| "-7777777777" |
812156815 |
-7777777777 超出了 long 长整数范围:-2147483648 ~ 2147483647,得到错误的结果 |
| "-1111111111" |
-1111111111 |
|
| "123e45" |
123 |
终止于不可识别的字符 'e',前面的内容转换结果为 123 |
| "a56789" |
0 |
第一个字符不可识别,转换结果为 0 |
例子:
void __fastcall TForm1::Button1Click(TObject *Sender)
{
UnicodeString s;
Memo1->Lines->Add(s.sprintf(L"_wtol(L\" 12345\") = %d", _wtol(L" 12345") ));
Memo1->Lines->Add(s.sprintf(L"_wtol(L\"-54321\") = %d", _wtol(L"-54321") ));
Memo1->Lines->Add(s.sprintf(L"_ttol(_T(\"2000000000\")) = %d", _ttol(_T("2000000000")) ));
Memo1->Lines->Add(s.sprintf(L"_ttol(_T(\"3000000000\")) = %d", _ttol(_T("3000000000")) ));
Memo1->Lines->Add(s.sprintf(L" atol(\"-7777777777\") = %d", atol("-7777777777") ));
Memo1->Lines->Add(s.sprintf(L" atol(\"-1111111111\") = %d", atol("-1111111111") ));
Memo1->Lines->Add(s.sprintf(L"_wtol(L\"123e45\") = %d", _wtol(L"123e45") ));
Memo1->Lines->Add(s.sprintf(L"_wtol(L\"a56789\") = %d", _wtol(L"a56789") ));
} |

兼容性:
| 函数 \ C++ Builder 编译器 |
bcc32 |
clang32 |
clang64 |
| atol |
√ |
√ |
√ |
| _ttol |
√ |
√ |
√ |
| _wtol |
√ |
√ |
√ |
相关链接:
• atoi • atol • _atoi64 • strtol • strtoll • strtoul • strtoull
• itoa • ltoa • ultoa • _i64toa • _ui64toa
• IntToStr • UIntToStr • IntToHex
• StrToInt • StrToIntDef • StrToInt64 • StrToInt64Def • StrToUInt64 • StrToUInt64Def
• EnumToStr • StrToEnum
|
|
|
|