Re: [問題] wchar_t問題求解
看板C_and_CPP (C/C++)作者descent (「雄辯是銀,沉默是金」)時間11年前 (2015/03/05 18:10)推噓3(3推 0噓 2→)留言5則, 2人參與討論串2/2 (看更多)
※ 引述《x049 (FD)》之銘言:
: 小弟之前發過一篇詢問字串轉16*16 bitmap的方法
: https://www.ptt.cc/bbs/C_and_CPP/M.1421124353.A.575.html
: 但是在學習freetype時參考下列文章,遇到了寬字符wchar_t類型出現問題
: http://yycking.blogspot.tw/2010/12/freetypebitmap.html
: 錯誤訊息是[Error] converting to execution character set: Illegal byte sequence
: 在main內的if(rasters(L"新年快樂", &bitmap))此行出現錯誤,游標在L那裡閃爍
: 使用的是 DEV c++ 編譯器
: 有去爬文都說在編譯器加入命令轉成UTF-8編碼,可是還是會出現錯誤訊息,
: 希望有大大能解答。
你要傳 wchar_t 這種 type, utf8 c-style string 要先轉 wchar_t,
可以使用 c++ 11 的 unicode class
str 是以 utf8 encoding 的 string
std::string wstring_to_utf8 (const std::wstring& str)
{
std::wstring_convert<std::codecvt_utf8<wchar_t>> myconv;
return myconv.to_bytes(str);
}
std::wstring utf32_str = utf8_to_wstring(disp_str);
utf32_str[i] 這樣傳進去。
gcc c++11 不能用這樣的方法,
我是用 clang with libc++
clang -stdlib=libc++ -std=c++11 ex.cpp
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 36.236.217.244
※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1425550229.A.BF1.html
→
03/05 18:24, , 1F
03/05 18:24, 1F
做個小修正
推
03/06 00:57, , 2F
03/06 00:57, 2F
你需要將 utf8 -> ucs4 才能傳給 freetype function FT_Get_Char_Index,
上述方法是我知道的 c++ 轉換方式。
我就是用這樣的方式使用 freetype, 也可以正確將字型中的文字畫出來。
該範例中的 const wchar_t texts[] 需要 ucs4 而不是 utf8。
你用 c 的話要自己找方法把 utf8 轉 ucs4。
你可以先用 20013, 25991 當作 texts input,
這是《中》 《文》的 ucs4 碼,
應該會得到 《中》 《文》 這兩個字。
以下是我的輸出範例
||||||||*||||||||||
||||||||**|||||||||
||||||||*||||||||||
||||||||*||||||||||
||||||||*||||||||||
*******************
*|||||||*||||||||*|
*|||||||*||||||||*|
*|||||||*||||||||*|
*|||||||*||||||||*|
*|||||||*||||||||*|
*|||||||*||||||||*|
*|||||||*||||||||*|
*|||||||*||||||||*|
******************|
*|||||||*||||||||*|
||||||||*||||||||||
||||||||*||||||||||
||||||||*||||||||||
||||||||*||||||||||
||||||||*||||||||||
||||||||*||||||||||
||||||||*||||||||||
|||||||||||||||||||
|||||||||||**|||||||||
|||||||||||*||||||||||
|||||||||||*||||||||||
|||||||||||*|||||||**|
**********************
|||||||||||||||**|||||
||||||*||||||||**|||||
|||||**||||||||*||||||
|||||**||||||||*||||||
||||*||*|||||||*||||||
|||*|||*||||||*|||||||
|||*||||*|||||*|||||||
|||||||||*|||*||||||||
|||||||||*||*|||||||||
||||||||||***|||||||||
||||||||||**||||||||||
|||||||||**|**||||||||
||||||||*||||**|||||||
||||||**|||||||**|||||
||||**||||||||||****||
|***||||||||||||||****
*|||||||||||||||||||||
※ 編輯: descent (36.236.214.210), 03/06/2015 10:05:10
推
03/06 20:05, , 3F
03/06 20:05, 3F
→
03/06 20:08, , 4F
03/06 20:08, 4F
https://github.com/descent/progs/tree/master/cpp11_utf8
這是我的程式碼 by c++ 參考看看。
※ 編輯: descent (180.217.249.241), 03/06/2015 22:32:17
推
03/07 16:50, , 5F
03/07 16:50, 5F
你可以把 svgalib 移掉, 裡頭有 console 的版本, 只使用 standard io
※ 編輯: descent (182.234.91.197), 03/07/2015 18:09:22
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 2 之 2 篇):
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章