atoi()与其他语言
atoi() with other languages
我正在进行一个国际化项目。除了0-9之外,其他语言(如阿拉伯语或汉语)是否对数字使用不同的表示法?如果是这样,是否有atoi()的版本可以解释这些其他表示?
我应该补充一点,我主要关心解析来自用户的输入。如果用户输入其他表示形式,我希望确保我将其识别为一个数字,并相应地对待它。
我可以使用std::wistringstream
和语言环境来生成这个整数。
#include <sstream>
#include <locale>
using namespace std;
int main()
{
locale mylocale("en-EN"); // Construct locale object with the user's default preferences
wistringstream wss(L"1"); // your number string
wss.imbue( mylocale ); // Imbue that locale
int target_int = 0;
wss >> target_int;
return 0;
}
有关流类和区域设置类的详细信息。
如果您关心国际字符,则需要确保使用"Unicode感知"函数,如_wtoi(..).
您还可以检查是否支持UNICODE使其与类型无关(来自MSDN):
TCHAR tstr[4] = TEXT("137");
#ifdef UNICODE
size_t cCharsConverted;
CHAR strTmp[SIZE]; // SIZE equals (2*(sizeof(tstr)+1)). This ensures enough
// room for the multibyte characters if they are two
// bytes long and a terminating null character. See Security
// Alert below.
wcstombs_s(&cCharsConverted, strTmp, sizeof(strTmp), (const wchar_t *)tstr, sizeof(strTmp));
num = atoi(strTmp);
#else
int num = atoi(tstr);
#endif
在本例中,标准C库函数wcstombs翻译Unicode到ASCII。该示例依赖于关于数字0到9总是可以从Unicode到ASCII,即使周围文字不能。atoi函数在任何字符处停止不是数字。
您的应用程序可以使用语言支持(NLS)LCMapString函数来处理包含为某些Unicode中的脚本。
小心使用wcstobbs函数不正确可能会危及应用程序的安全性。制作确保的应用程序缓冲区8位字符的字符串位于大小2*(char_length+1)中的最小值,其中char_length表示Unicode字符串的长度。这作出限制是因为双字节字符集(DBCS),每个Unicode字符都可以映射转换为两个连续的8位字符。如果缓冲区不能容纳整个字符串,结果字符串不是null终止,构成安全危险有关的详细信息应用程序安全性,请参阅安全性考虑因素:国际功能。
相关文章:
- Java 和其他语言在生成子序列时的输出差异
- 与其他语言相比的 Ruby 和指针
- 其他语言的Qt键盘输入
- 我需要知道什么才能开始用c++/其他语言编程合成器
- 动态C++与其他语言的比较 - 在不知道类型的情况下无法在对象上调用方法
- 用java编写源代码,并将其编译为其他语言的各种可执行文件?多重编译
- 如何使C枚举类型可用于其他语言
- 使用大多数Java代码制作可执行文件,但可能添加了其他语言
- 如何用其他语言编写带有绑定的Qt插件系统
- 库在C/C++和其他语言中重现Java原语hashCode逻辑
- WebAPI2 Async .是与所有其他语言兼容的
- 从 bash 拆分的流是否可以转置为其他语言
- 用PHP或任何其他语言上传巨大的文件
- 如何在Ruby和其他语言之间共享文本文件(或互斥/信号量)
- 如何通过C++(或建议其他语言)在网页上"do stuff"
- 在GO中使库可以从其他语言中使用
- 这在C++、PHP和其他语言中的使用
- 为什么C/C++有不同于C#和Java等其他语言的头文件
- C/ c++(其他语言?)条件提前返回良好的代码实践
- 如何制作一个古怪的数学计算器?(最好使用c++,但其他语言也可以)