atoi()与其他语言

atoi() with other languages

本文关键字:其他 语言 atoi      更新时间:2023-10-16

我正在进行一个国际化项目。除了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终止,构成安全危险有关的详细信息应用程序安全性,请参阅安全性考虑因素:国际功能。