C library function - mbstowcs()
Description
C Library Function mbstowcs Used to convert a multibyte string into a wide-character string.
mbstowcs The function is defined in
Declaration
Here is the declaration of the mbstowcs() function.
size_t mbstowcs(wchar_t *pwcs, const char *mbs, size_t n);
Parameters
pwcs: a pointer to a buffer used to store the converted wide-character string. Ifpwcsis NULL, the function returns the required buffer length without performing the actual conversion.mbs: a pointer to the multibyte string.n: the maximum number of characters to convert. Ifncharacters are converted before a terminating character is encountered, conversion stops early.
Return Value
- On success, returns the number of wide characters converted, not including the terminating null character.
- If an invalid multibyte sequence is encountered, returns
(size_t)-1and setserrno.
Example
The following example demonstrates the use of the mbstowcs() function.
Example 1
#include
<
stdio.h
#include
<
stdlib.h
#include
<
string.h
int
main
(
)
{
int
len;
char
pmbnull
=
NULL
;
char
pmb
=
(
char
)
malloc
(
MB_CUR_MAX
)
;
wchar_t
pwc
=
L
"
Hi
"
;
wchar_t
pwcs
=
(
wchar_t
)
malloc
(
sizeof
(
wchar_t
)
)
;
printf
(
"
Convert to a multibyte string
\
n
"
)
;
len
=
wcstombs
(
pmb
,
pwc
,
MB_CUR_MAX
)
;
printf
(
"
Characters converted: %d
\
n
"
,
len
)
;
printf
(
"
Hexadecimal value of the first multibyte character: %#.4x
\
n
"
,
pmb
)
;
printf
(
"
Convert back to a wide-character string
\
n
"
)
;
len
=
mbstowcs
(
pwcs
,
pmb
,
MB_CUR_MAX
)
;
printf
(
"
Characters converted: %d
\
n
"
,
len
)
;
printf
(
"
Hexadecimal value of the first wide character: %#.4x
\
n
\
n
"
,
pwcs
)
;
return
(
0
)
;
}
Let's compile and run the above program, which will produce the following result:
转换为多字节字符串
被转换的字符 1
第一个多字节字符的十六进制值:0x19a60010
转换回宽字符字符串
被转换的字符 1
第一个宽字符的十六进制值:0x19a60030
Example 2
#include
#include
#include
#include
int
main
(
)
{
// Set the current locale to the one specified by the user environment variables
setlocale
(
LC_ALL
,
""
)
;
// Define the multibyte string
const
char
mbstr
=
"Hello, World!"
;
// Compute the required buffer size
size_t
wcs_len
=
mbstowcs
(
NULL
,
mbstr
,
0
)
1
;
// Allocate the wide-character buffer
wchar_t
wcs
=
(
wchar_t
)
malloc
(
wcs_len
sizeof
(
wchar_t
)
)
;
if
(
wcs
==
NULL
)
{
perror
(
"malloc"
)
;
return
1
;
}
// Perform the conversion
mbstowcs
(
wcs
,
mbstr
,
wcs_len
)
;
// Print the wide-character string
wprintf
(
L
"%ls\n"
,
wcs
)
;
// Free memory
free
(
wcs
)
;
return
0
;
}
Let's compile and run the above program, which will produce the following result:
Hello, 世界!
Notes
- Locale: the function's behavior is affected by the current
localesetting. Usually, before calling the function, it ismbstowcsnecessary to callsetlocaleto set the appropriatelocale. - Error handling: If the input multibyte string is malformed or cannot be converted to a wide character, the conversion fails.
(size_t)-1and setserrnothe error type. The caller needs to check the return value and handle possible errors. - memory allocation: the caller needs to ensure that
pwcshas enough space to store the converted wide-character string. This can be done by callingmbstowcs, and replacepwcstoNULLonce to compute the required buffer size.
C Standard Library -