unicode - Number of bytes of CString in C++ -
i have unicode string stored in cstring , need know number bytes string takes in utf-8 encoding. know cstring has method getlength()
, returns number of charactes, not bytes.
i tried (beside other things) converting char array, (logically, guess) array of wchar_t
, doesn't solve problem.
to clear goal. input lets "aaa"
want "3" output (since "a" takes 1 byte in utf-8). input "āaa", i'd see output "4" (since ā 2 byte character).
i think has quite common request, after 1,5 hours of search , experimenting, couldn't find correct solution.
i have little experience windows programming, maybe left out crucial information. if feel that, please let me know, i'll add information request.
use widechartomultibyte output charset cp_utf8
the function return number of bytes written output buffer, or length of utf-8 encoded string
lpcstr instr; char outstr[max_outstr_size]; int utf8_len = widechartomultibyte(cp_utf8, 0, instr, -1, outstr, max_outstr_size, null, null);
after converting can use strlen
on output string
int utf8_len = strlen(outstr);
if don't need output string, can set output buffer size 0. in case function return number of bytes in utf-8 without outputting anything
int utf8_len = widechartomultibyte(cp_utf8, 0, instr, -1, null, 0, null, null);
Comments
Post a Comment