[英]How to convert unicode code points to utf-8 in c++?
我有一个由 unicode 代码点组成的数组
unsigned short array[3]={0x20ac,0x20ab,0x20ac};
我只是希望将其转换为 utf-8 以使用 C++ 逐字节写入文件。
示例:0x20ac 应转换为 e2 82 ac。
或者有没有其他方法可以直接在文件中写入unicode字符。
终于! 使用 C++11!
#include <string>
#include <locale>
#include <codecvt>
#include <cassert>
int main()
{
std::wstring_convert<std::codecvt_utf8<char32_t>, char32_t> converter;
std::string u8str = converter.to_bytes(0x20ac);
assert(u8str == "\xe2\x82\xac");
}
以下代码可能对您有所帮助,
#include <atlconv.h>
#include <atlstr.h>
#define ASSERT ATLASSERT
int main()
{
const CStringW unicode1 = L"\x0391 and \x03A9"; // 'Alpha' and 'Omega'
const CStringA utf8 = CW2A(unicode1, CP_UTF8);
ASSERT(utf8.GetLength() > unicode1.GetLength());
const CStringW unicode2 = CA2W(utf8, CP_UTF8);
ASSERT(unicode1 == unicode2);
}
Unicode一词指的是一种用于编码和处理文本的标准。 这结合了UTF-8 、 UTF-16 、 UTF-32 、 UCS-2 等编码......
我猜您是在 Windows 环境中编程,其中Unicode通常是指UTF-16 。
在 C++ 中使用 Unicode 时,我会推荐ICU 库。
如果您在 Windows 上编程,不想使用外部库,并且对平台依赖性没有限制,则可以使用WideCharToMultiByte
。
重症监护病房示例:
#include <iostream>
#include <unicode\ustream.h>
using icu::UnicodeString;
int main(int, char**) {
//
// Convert from UTF-16 to UTF-8
//
std::wstring utf16 = L"foobar";
UnicodeString str(utf16.c_str());
std::string utf8;
str.toUTF8String(utf8);
std::cout << utf8 << std::endl;
}
做你想做的事:
// Assuming you have ICU\include in your include path
// and ICU\lib(64) in your library path.
#include <iostream>
#include <fstream>
#include <unicode\ustream.h>
#pragma comment(lib, "icuio.lib")
#pragma comment(lib, "icuuc.lib")
void writeUtf16ToUtf8File(char const* fileName, wchar_t const* arr, size_t arrSize) {
UnicodeString str(arr, arrSize);
std::string utf8;
str.toUTF8String(utf8);
std::ofstream out(fileName, std::ofstream::binary);
out << utf8;
out.close();
}
此代码使用WideCharToMultiByte
(我假设您使用的是 Windows):
unsigned short wide_str[3] = {0x20ac, 0x20ab, 0x20ac};
int utf8_size = WideCharToMultiByte(CP_UTF8, 0, wide_str, 3, NULL, 0, NULL, NULL) + 1;
char* utf8_str = calloc(utf8_size);
WideCharToMultiByte(CP_UTF8, 0, wide_str, 3, utf8_str, utf8_size, NULL, NULL);
您需要调用它两次:第一次获取输出字节数,第二次实际转换它。 如果您知道输出缓冲区大小,则可以跳过第一次调用。 或者,您可以简单地分配比原始缓冲区大 2 倍 + 1 个字节的缓冲区(对于您的情况,这意味着 12+1 个字节) - 它应该总是足够的。
您可以使用Boost.Locale of Boost库: http : //www.boost.org/doc/libs/1_55_0/libs/locale/doc/html/index.html
使用标准 C++
#include <iostream>
#include <locale>
#include <vector>
int main()
{
typedef std::codecvt<wchar_t, char, mbstate_t> Convert;
std::wstring w = L"\u20ac\u20ab\u20ac";
std::locale locale("en_GB.utf8");
const Convert& convert = std::use_facet<Convert>(locale);
std::mbstate_t state;
const wchar_t* from_ptr;
char* to_ptr;
std::vector<char> result(3 * w.size() + 1, 0);
Convert::result convert_result = convert.out(state,
w.c_str(), w.c_str() + w.size(), from_ptr,
result.data(), result.data() + result.size(), to_ptr);
if (convert_result == Convert::ok)
std::cout << result.data() << std::endl;
else std::cout << "Failure: " << convert_result << std::endl;
}
Iconv是在许多平台上使用的流行库。
我有一个类似但略有不同的问题。 我将带有 Unicode 代码点的字符串作为字符串表示。 例如:“F\ó\ó B\ár”。 我需要将字符串代码点转换为它们的 Unicode 字符。
这是我的 C# 解决方案
using System.Globalization;
using System.Text.RegularExpressions;
static void Main(string[] args)
{
Regex CodePoint = new Regex(@"\\u(?<UTF32>....)");
Match Letter;
string s = "F\u00f3\u00f3 B\u00e1r";
string utf32;
Letter = CodePoint.Match(s);
while (Letter.Success)
{
utf32 = Letter.Groups[1].Value;
if (Int32.TryParse(utf32, NumberStyles.HexNumber, CultureInfo.GetCultureInfoByIetfLanguageTag("en-US"), out int HexNum))
s = s.Replace("\\u" + utf32, Char.ConvertFromUtf32(HexNum));
Letter = Letter.NextMatch();
}
Console.WriteLine(s);
}
输出:FóóBár
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.