Ben Bacarisse
4/10/2011 9:08:00 PM
Owner <Owner@Owner-PC.com> writes:
> How to determine Multibyte string length?
>
> So that I can allocate memory size equal to mulibyte string length.
>
> I found there is mblen but it's only work for single character.
>
> any help wouldbe appreciated
The question is a little under specified -- in particular about what to
do when the string is malformed. Assuming that we can do anything at all
in this case one answer wold be to use the mbsrtowcs function (that's
"multi-byte string, restartable, to wide character string").
Apart from having rather too many parameters, the second must be a
pointer to a pointer so it really helps to wrap this function like this:
size_t mbslen(const char *s)
{
return mbsrtowcs(NULL, &s, 0, &(mbstate_t){0});
}
No doubt you have worked out that one could also just iterate calls to
mblen, but I am not sure there is anything to be gained from that
method.
--
Ben.