簡體   English   中英

UTF-8字符轉換

[英]UTF-8 conversion for characters

我目前有一個std::string ,它包含這個

"\xa9 2006 FooWorld"

基本上,它包含符號©。 該字符串將傳遞給采用UTF-8的外部API的方法。 如何使此字符串UTF-8兼容? 有什么建議么。 在這里讀到我可以使用std::wstring_convert但是我不確定如何在我的情況下應用它。 任何建議,將不勝感激。

這很簡單:使用UTF-8字符串文字:

u8"\u00A9 2006 FooWorld"

這將導致const char[] ,它是正確編碼的UTF-8字符串。

在C ++ 11和更高版本中,獲取UTF-8編碼的字符串文字的最佳方法是使用u8前綴:

std:string str = u8"\u00A9 2006 FooWorld";

要么:

std:string str = u8"© 2006 FooWorld";

但是,您也可以使用std::wstring_convert (尤其是如果您的輸入數據不是字符串文字):

#include <codecvt>
#include <locale>
#include <string>

std::wstring wstr = L"© 2006 FooWorld"; // or whatever...

std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t> convert;

std::string str = convert.to_bytes(wstr);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM