C library functions -mbstowcs()
C Standard Library - <stdlib.h>
Description
C Library FunctionsmbstowcsUsed to convert a multibyte string to a wide character string.
mbstowcsThe function is defined in the <stdlib.h> header file.
Declaration
Below is the declaration of the mbstowcs() function.
size_t mbstowcs(wchar_t *pwcs, const char *mbs, size_t n);
Parameter
pwcs: Points 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: Pointer to a multibyte string.n: The maximum number of characters to convert. If, before encountering the terminating character, the conversion has already been performedncharacters, the conversion stops early.
Return Value
- On success, returns the number of wide characters converted, excluding the terminating null character.
- If an invalid multibyte sequence is encountered, returns
(size_t)-1, and seterrno。
Example
The following example demonstrates the usage 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("Converted character %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("Converted character %d\n", len);
printf("Hexadecimal value of first wide character: %#.4x\n\n", pwcs);
return(0);
}
Let us compile and run the above program, which will produce the following result:
转换为多字节字符串 被转换的字符 1 第一个多字节字符的十六进制值:0x19a60010 转换回宽字符字符串 被转换的字符 1 第一个宽字符的十六进制值:0x19a60030
Example 2
#include <stdio.h>
#include <stdlib.h>
#include <wchar.h>
#include <locale.h>
int main() {
// Set the current locale to the locale specified by the user environment variable
setlocale(LC_ALL, "");
// Define a multibyte string
const char *mbstr = "Hello, world!";
// Calculate the required buffer size
size_t wcs_len = mbstowcs(NULL, mbstr, 0) + 1;
// Allocate wide character buffer
wchar_t *wcs = (wchar_t *)malloc(wcs_len * sizeof(wchar_t));
if (wcs == NULL) {
perror("malloc");
return 1;
}
// Perform conversion
mbstowcs(wcs, mbstr, wcs_len);
// Print wide character string
wprintf(L"%ls\n", wcs);
// release memory
free(wcs);
return 0;
}
#include <stdlib.h>
#include <wchar.h>
#include <locale.h>
int main() {
// Set the current locale to the locale specified by the user environment variable
setlocale(LC_ALL, "");
// Define a multibyte string
const char *mbstr = "Hello, world!";
// Calculate the required buffer size
size_t wcs_len = mbstowcs(NULL, mbstr, 0) + 1;
// Allocate wide character buffer
wchar_t *wcs = (wchar_t *)malloc(wcs_len * sizeof(wchar_t));
if (wcs == NULL) {
perror("malloc");
return 1;
}
// Perform conversion
mbstowcs(wcs, mbstr, wcs_len);
// Print wide character string
wprintf(L"%ls\n", wcs);
// release memory
free(wcs);
return 0;
}
Let us compile and run the above program, which will produce the following result:
Hello, 世界!
Notes
- Locale: The behavior of the function is affected by the current
localesetting, usually when callingmbstowcsNeed to call beforesetlocaleto set the appropriatelocale。 - Error Handling: If the input multibyte string contains an invalid multibyte sequence, the function returns
(size_t)-1and seterrno. The caller needs to check the return value and handle possible errors. - Memory allocation: The caller needs to ensure
pwcsThere is enough space to store the converted wide character string. This can be done by first callingmbstowcs, thenpwcsSet toNULLto calculate the required buffer size.