function
<cwchar>
mbrtowcsize_t mbrtowc (wchar_t* pwc, const char* pmb, size_t max, mbstate_t* ps);
Convert multibyte sequence to wide character
The multibyte character pointed by pmb is converted to a value of type wchar_t and stored at the location pointed by pwc. The function returns the length in bytes of the multibyte character.The function uses (and updates) the shift state described by ps. If ps is a null pointer, the function uses its own internal shift state, which is altered as necessary only by calls to this function.
If pmb points to a null character, the function resets the shift state and returns zero after storing the wide null character at pwc.
A call to the function with a null pointer as pmb also resets the shift state, ignoring parameters pwc and max (no character is stored ad pwc).
The behavior of this function depends on the LC_CTYPE category of the selected C locale.
This is the restartable version of mbtowc (<cstdlib>).
If this was the null wide character, or if pmb is a null pointer, the function returns zero (in the first case, the null wide character is stored at pwc).
If the max first characters of pmb form an incomplete (but potentially valid) multibyte character, the function returns (size_t)-2 (no value is stored at pwc).
Otherwise, if the characters pointed by pmb do not form a valid multibyte character (or the beginning of one), the function returns (size_t)-1 and sets errno to EILSEQ (no value is stored at pwc).
Notice that size_t is an unsigned integral type, and thus none of the values possibly returned is less than zero.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/* mbrtowc example */
#include <wchar.h>
void printbuffer (const char* pt, size_t max)
{
size_t length;
wchar_t dest;
mbstate_t mbs;
mbrlen ( NULL, 0, &mbs ); /* initialize mbs */
while (max>0) {
length = mbrtowc(&dest,pt,max,&mbs);
if ((length==0)||(length>max)) break;
wprintf (L"[%lc]",dest);
pt+=length; max-=length;
}
}
int main()
{
const char str [] = "mbrtowc example";
printbuffer (str,sizeof(str));
return 0;
}
The example uses a trivial string on the "C" locale, but locales supporting multibyte string are supported by the function.
Output:
[m][b][r][t][o][w][c][ ][e][x][a][m][p][l][e]
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4