C library function - mbtowc()
mbtowc() is a function in the C standard library used to convert a multibyte character to a wide character.
mbtowc() Its declaration is located in header file, mainly used to support conversion between multibyte character sets (such as UTF-8) and wide character sets (such as Unicode).
mbtowc() Converts a multibyte character (mbstr) to a wide character (wc). The function determines how to interpret the multibyte character based on the current LC_CTYPE locale (environment setting). If the multibyte character is valid, it is converted to a wide character. If the character is invalid, it returns -1.
Declaration
Here is the declaration of the mbtowc() function.
#include
int mbtowc(wchar_t *wc, const char *mbstr, size_t n);
Parameters:
- wc: a pointer to a
wchar_ttype variable that receives the converted wide character. - mbstr: a pointer to the multibyte string, the sequence of multibyte characters to be converted.
- n: indicates the
mbstrmaximum number of bytes in the string. Usually you can set it to the length of the string.
Return Value
- On success,
mbtowc()returns the number of bytes occupied by the converted character (i.e., the number of bytes of the multibyte character inmbstr). Ifwcis notNULL, this value is greater than 0. - If an error occurs (such as an invalid multibyte character), the function returns
-1. - If the end of the character sequence is encountered (for example, the string terminator
\0), returns0.
Example
The following example demonstrates the use of the mbtowc() function.
Example
#include
#include
#include
#include
#include
int
main
(
)
{
// Set the program's locale information
setlocale
(
LC_CTYPE
,
""
)
;
// Multibyte string
const
char
str
=
"This is runoob.com"
;
// Wide-character array to store the converted result
wchar_t
mb
[
100
]
;
// Current character index and converted wide-character length
int
i
=
0
;
int
len
;
// Convert characters one by one to wide characters
while
(
str
)
{
len
=
mbtowc
(
&
mb
[
i
]
,
str
,
MB_CUR_MAX
)
;
// Convert the current multibyte character
if
(
len
0
)
{
str
+=
len
;
// Move to the start of the next character
i
++;
// Store into the wide-character array
}
else
if
(
len
==
0
)
{
break
;
// String terminator encountered
}
else
{
// Error handling for invalid characters
printf
(
"invalid character\n"
)
;
break
;
}
}
// Output the converted wide-character string
wprintf
(
L
"%ls \n"
,
mb
)
;
return
0
;
}
Let us compile and run the above program, which will produce the following result, because it outputs in multibyte form, which is a kind of binary output.
这里是 runoob.com
Example
#include
#include
#include
int
main
(
)
{
// Set the program's locale information
setlocale
(
LC_CTYPE
,
""
)
;
const
char
mbstr
=
"Hello" (Chinese)
;
wchar_t
wc
;
// Convert one character at a time
for
(
size_t
i
=
0
;
mbstr
[
i
]
!=
'\0'
;
i
++
)
{
int
len
=
mbtowc
(
&
wc
,
&
mbstr
[
i
]
,
1
)
;
if
(
len
0
)
{
wprintf
(
L
"converted wide character: %lc\n"
,
wc
)
;
i
+=
len
1
;
// Skip the number of bytes of the current multibyte character
}
else
if
(
len
==
0
)
{
break
;
}
else
{
printf
(
"invalid character\n"
)
;
}
}
return
0
;
}
Explanation:
-
Multibyte character sets: In a multibyte character set (such as UTF-8), one character may consist of multiple bytes, while in a wide character set (such as Unicode), one character is usually represented by a
wchar_ttype value (2 or 4 bytes on some platforms). -
Locale settings:
mbtowc()behavior depends on the current program's locale setting, especially theLC_CTYPEsetting, which determines how multibyte character sets are interpreted. Therefore, when using this function, it is usually necessary to set the appropriate locale viasetlocale()to set the appropriate locale. -
Error handling: If the input multibyte string is malformed or cannot be converted to a wide character, the conversion fails.
mbtowc()returns-1You should check the return value to ensure the conversion succeeded.
C Standard Library -