Latest web development tutorials

C library functions - mbstowcs ()

C standard library - <stdlib.h> C standard library - <stdlib.h>

description

C library functionssize_t mbstowcs (schar_t * pwcs, const char * str, size_t n) to convert a string argument strpoints for multi-byte character array pointedpwcsparameters.

statement

Here is () statement mbstowcs function.

size_t mbstowcs(schar_t *pwcs, const char *str, size_t n)

parameter

  • pwcs - pointing to a wchar_t array, the array length of the elements is sufficient to store a wide string maximum character length.
  • str - multi-byte character string to be converted.
  • n - maximum number of characters to be converted.

return value

This function returns the number of characters in the conversion, not including the terminating null character. If you encounter an invalid multi-byte characters, the return value of -1.

Examples

The following example demonstrates mbstowcs () function is used.

#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("转换为多字节字符串\n");
   len = wcstombs( pmb, pwc, MB_CUR_MAX);
   printf("被转换的字符 %d\n", len);
   printf("第一个多字节字符的十六进制值:%#.4x\n", pmb);
   
   printf("转换回宽字符字符串\n");
   len = mbstowcs( pwcs, pmb, MB_CUR_MAX);
   printf("被转换的字符 %d\n", len);
   printf("第一个宽字符的十六进制值:%#.4x\n\n", pwcs);
   
   return(0);
}

Let's compile and run the above program, which will result in the following:

转换为多字节字符串
被转换的字符 1
第一个多字节字符的十六进制值:0x19a60010
转换回宽字符字符串
被转换的字符 1
第一个宽字符的十六进制值:0x19a60030

C standard library - <stdlib.h> C standard library - <stdlib.h>