C Library Function β mbstowcs()
\n\nDescription
\nThe C library function mbstowcs is used to convert a multibyte string to a wide character string.
\nThe mbstowcs function is defined in the <stdlib.h> header file.
Declaration
\nBelow is the declaration for the mbstowcs() function.
size_t mbstowcs(wchar_t *pwcs, const char *mbs, size_t n);\n\n Parameters
\n- \n
pwcs: A pointer to a buffer where the converted wide character string will be stored. Ifpwcsis NULL, the function returns the required buffer length without performing the actual conversion. \n mbs: A pointer to the multibyte string. \n n: The maximum number of characters to convert. Ifncharacters have been converted before encountering the null terminator, the conversion stops early. \n
Return Value
\n- \n
- On success, it returns the number of wide characters converted, excluding the terminating null character. \n
- If an invalid multibyte sequence is encountered, it returns
(size_t)-1and setserrno. \n
Example
\nThe following example demonstrates the usage of the mbstowcs() function.
Example 1
\n#include<stdio.h>\n#include<stdlib.h>\n#include<string.h>\n\nint main()\n{\n int len;\n char *pmbnull = NULL;\n char *pmb = (char *)malloc(MB_CUR_MAX);\n wchar_t *pwc = L"Hi";\n wchar_t *pwcs = (wchar_t *)malloc(sizeof(wchar_t));\n\n printf("Converting to multibyte stringn");\n len = wcstombs(pmb, pwc, MB_CUR_MAX);\n printf("Characters converted %dn", len);\n printf("Hexadecimal value of first multibyte char: %#.4xn", pmb);\n\n printf("Converting back to wide character stringn");\n len = mbstowcs(pwcs, pmb, MB_CUR_MAX);\n printf("Characters converted %dn", len);\n printf("Hexadecimal value of first wide char: %#.4xnn", pwcs);\n\n return(0);\n}\n Let us compile and run the above program, this will produce the following result:
\nConverting to multibyte string\nCharacters converted 1\nHexadecimal value of first multibyte char: 0x19a60010\nConverting back to wide character string\nCharacters converted 1\nHexadecimal value of first wide char: 0x19a60030\n\n Example 2
\n#include <stdio.h>\n#include <stdlib.h>\n#include <wchar.h>\n#include <locale.h>\n\nint main() {\n // Set the current locale to the user's environment variable locale\n setlocale(LC_ALL, "");\n\n // Define a multibyte string\n const char* mbstr = "Hello, World!";\n\n // Calculate the required buffer size\n size_t wcs_len = mbstowcs(NULL, mbstr, 0) + 1;\n\n // Allocate wide character buffer\n wchar_t* wcs = (wchar_t*)malloc(wcs_len * sizeof(wchar_t));\n if (wcs == NULL) {\n perror("malloc");\n return 1;\n }\n\n // Perform the conversion\n mbstowcs(wcs, mbstr, wcs_len);\n\n // Print the wide character string\n wprintf(L"%lsn", wcs);\n\n // Free the allocated memory\n free(wcs);\n\n return 0;\n}\n Let us compile and run the above program, this will produce the following result:
\nHello, World!\n\n Notes
\n- \n
- Locale: The function's behavior is affected by the current
localesetting. Typically, you need to callsetlocalebefore callingmbstowcsto set an appropriatelocale. \n - Error Handling: If the input multibyte string contains an invalid multibyte sequence, the function returns
(size_t)-1and setserrno. The caller needs to check the return value and handle potential errors. \n - Memory Allocation: The caller must ensure that
pwcshas enough space to store the converted wide character string. You can calculate the required buffer size by callingmbstowcsonce withpwcsset toNULL. \n
YouTip