atoi()与其他语言 [英] atoi() with other languages

查看:116
本文介绍了atoi()与其他语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从事国际化项目.除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;
}

有关流类的详细信息关于语言环境类. /p>

I am working on a internationalization project. Do other languages, such as Arabic or Chinese, use different representations for digits besides 0-9? If so, are there versions of atoi() that will account for these other representations?

I should add that I am mainly concerned with parsing input from the user. If the users types in some other representation I want to be sure that I recognize it as a number and treat it accordingly.

解决方案

I may use std::wistringstream and locale to generate this integer.

#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;
}

More info on stream class and on locale class.

这篇关于atoi()与其他语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆