如何使用标准C ++从UTF-8转换为ANSI [英] How to convert from UTF-8 to ANSI using standard c++
问题描述
我从数据库中读取了一些字符串,它们以char *和UTF-8格式存储(您知道,á的编码为0xC3 0xA1)。但是,为了将它们写到文件中,我首先需要将它们转换为ANSI(无法将文件制成UTF-8格式...它只能读为ANSI),因此我的á不会成为一个。是的,我知道某些数据会丢失(中文字符,并且一般而言,ANSI代码页中没有的任何内容),但这正是我所需要的。
I have some strings read from the database, stored in a char* and in UTF-8 format (you know, "á" is encoded as 0xC3 0xA1). But, in order to write them to a file, I first need to convert them to ANSI (can't make the file in UTF-8 format... it's only read as ANSI), so that my "á" doesn't become "á". Yes, I know some data will be lost (chinese characters, and in general anything not in the ANSI code page) but that's exactly what I need.
但是问题是,我需要在各种平台上进行编译的代码,因此它必须是标准的C ++(即没有Winapi,只有stdlib,stl,crt或任何具有可用源代码的自定义库)。
But the thing is, I need the code to compile in various platforms, so it has to be standard C++ (i.e. no Winapi, only stdlib, stl, crt or any custom library with available source).
有人有什么建议吗?
推荐答案
几天前,有人回答说,如果我有C ++ 11编译器,我可以尝试这样做:
A few days ago, somebody answered that if I had a C++11 compiler, I could try this:
#include <string>
#include <codecvt>
#include <locale>
string utf8_to_string(const char *utf8str, const locale& loc)
{
// UTF-8 to wstring
wstring_convert<codecvt_utf8<wchar_t>> wconv;
wstring wstr = wconv.from_bytes(utf8str);
// wstring to string
vector<char> buf(wstr.size());
use_facet<ctype<wchar_t>>(loc).narrow(wstr.data(), wstr.data() + wstr.size(), '?', buf.data());
return string(buf.data(), buf.size());
}
int main(int argc, char* argv[])
{
string ansi;
char utf8txt[] = {0xc3, 0xa1, 0};
// I guess you want to use Windows-1252 encoding...
ansi = utf8_to_string(utf8txt, locale(".1252"));
// Now do something with the string
return 0;
}
不知道响应发生了什么,显然有人删除了它。但是,事实证明,这是完美的解决方案。对于张贴的任何人,非常感谢,您值得AC和支持!!!
Don't know what happened to the response, apparently someone deleted it. But, turns out that it is the perfect solution. To whoever posted, thanks a lot, and you deserve the AC and upvote!!
这篇关于如何使用标准C ++从UTF-8转换为ANSI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!