Let*_*_Be 15 c++ unicode locale utf-8
如果我想在Windows上进行以下工作,那么正确的语言环境是什么?如何检测它实际存在: 此代码是通用的,还是仅仅是我的系统?
Cub*_*bbi 12
尽管对命名语言环境没有很好的支持,但Visual Studio 2010确实包含了C++ 11所需的UTF-8转换方面:std::codecvt_utf8对于UCS2和std::codecvt_utf8_utf16UTF-16:
#include <fstream>
#include <iostream>
#include <string>
#include <locale>
#include <codecvt>
void prepare_file()
{
// UTF-8 data
char utf8[] = {'\x7a', // latin small letter 'z' U+007a
'\xe6','\xb0','\xb4', // CJK ideograph "water" U+6c34
'\xf0','\x9d','\x84','\x8b'}; // musical sign segno U+1d10b
std::ofstream fout("text.txt");
fout.write(utf8, sizeof utf8);
}
void test_file_utf16()
{
std::wifstream fin("text.txt");
fin.imbue(std::locale(fin.getloc(), new std::codecvt_utf8_utf16<wchar_t>));
std::cout << "Read from file using UTF-8/UTF-16 codecvt\n";
for(wchar_t c; fin >> c; )
std::cout << std::hex << std::showbase << c << '\n';
}
void test_file_ucs2()
{
std::wifstream fin("text.txt");
fin.imbue(std::locale(fin.getloc(), new std::codecvt_utf8<wchar_t>));
std::cout << "Read from file using UTF-8/UCS2 codecvt\n";
for(wchar_t c; fin >> c; )
std::cout << std::hex << std::showbase << c << '\n';
}
int main()
{
prepare_file();
test_file_utf16();
test_file_ucs2();
}
Run Code Online (Sandbox Code Playgroud)
这个输出,在我的Visual Studio 2010 EE SP1上
Read from file using UTF-8/UTF-16 codecvt
0x7a
0x6c34
0xd834
0xdd0b
Read from file using UTF-8/UCS2 codecvt
0x7a
0x6c34
0xd10b
Press any key to continue . . .
Run Code Online (Sandbox Code Playgroud)
Nem*_*vic 10
基本上,你运气不好:http://www.siao2.com/2007/01/03/1392379.aspx
过去不允许将 UTF-8(和一些其他代码页)作为系统区域设置,因为
微软表示 UTF-8 语言环境可能会破坏某些功能,因为它们被编写为假设多字节编码使用的每个字符不超过 2 个字节,因此具有更多字节的代码页,例如 UTF-8(以及 GB 18030、cp54936)不能设置为语言环境。
https://en.wikipedia.org/wiki/Unicode_in_Microsoft_Windows#UTF-8
然而,微软逐渐引入了UTF-8 语言环境支持,并开始-A再次推荐 ANSI API ( ) 而不是-W像以前那样的 Unicode ( ) 版本
直到最近,Windows 还强调“Unicode”
-W变体而不是-AAPI。但是,最近的版本使用 ANSI 代码页和-AAPI 作为向应用程序引入 UTF-8 支持的一种方式。如果 ANSI 代码页配置为 UTF-8,则-AAPI 以 UTF-8 运行。此模型的优点是支持使用-AAPI构建的现有代码,无需任何代码更改。-A 与 -W API
首先,他们添加了一个“测试版:使用 Unicode UTF-8 进行全球语言支持”复选框,因为 Windows 10 Insider build 17035 用于将区域设置代码页设置为 UTF-8
要打开该对话框,请打开开始菜单,键入“区域”并选择“区域设置”>“其他日期、时间和区域设置”>“更改日期、时间或数字格式”>“管理”
启用后,您可以setlocal正常调用:
从 Windows 10 内部版本 17134(2018 年 4 月更新)开始,通用 C 运行时支持使用 UTF-8 代码页。这意味着
char传递给 C 运行时函数的字符串将需要 UTF-8 编码的字符串。要启用 UTF-8 模式,请在使用setlocale. 例如,setlocale(LC_ALL, ".utf8")将使用当前默认的 Windows ANSI 代码页 (ACP) 作为语言环境,使用 UTF-8 作为代码页。
您也可以在较旧的 Windows 版本中使用它
若要在 Windows 10 之前的操作系统(例如 Windows 7)上使用此功能,必须使用应用本地部署或使用 Windows SDK 17134 版或更高版本静态链接。对于 17134 之前的 Windows 10 操作系统,仅支持静态链接。
2019 年晚些时候,他们增加了程序使用 UTF-8 语言环境的能力,甚至无需在上面设置 UTF-8 beta 标志。您可以在使用MSVC 编译时使用/execution-charset:utf-8或/utf-8选项或在 appxmanifest 中设置 ActiveCodePage 属性
| 归档时间: |
|
| 查看次数: |
19304 次 |
| 最近记录: |