The role of mbstowcs() function and wcstombs() function

Required header files:

#include <stdlib.h>

 

mbstowcs() function

Function: Convert multi-byte characters into wide bytes
Prototype:

size_t  mbstowcs(

     wchar_t   *wcstr,

     const char   *mbstr,

     size_t    count

);

Parameter Description:

  • wcstr target cache, used to store wide-byte characters after conversion; when the value is NULL, the return value is the number of wchar_t types required by the target cache
  • mbstr is used to store the multibyte string to be converted
  • count is used to specify the maximum number of bytes to be converted. When the value of wcst is NULL, the value is useless

return value:

  • When the conversion is successful and wcstr == NULL, return the size required by the target cache (the number of wchar_t types, but without the terminator);
  • When the conversion is successful and wcstd != NULL, the number of bytes converted is returned;
  • Conversion is not successful, return (size_t)(-1);

Steps for usage:

  • Call the mbstowcs() function and set the parameter wcstr to NULL (to obtain the size of the receiving buffer required for conversion);
  • Allocate enough memory blocks to the target buffer area to store the converted wchar_t string;

    Note: The size of the memory block is determined by the return value of the first call to the mbstowcs() function (the return value does not include the space for the terminator) 

  • Call the mbstowcs() function again, this time passing the address of the target cache as the wcstr parameter;

Code example:


	strcpy(sBuf, "我最棒");

	size_t sSize=strlen(sBuf);

	wchar_t * dBuf=NULL;


	//注意:需要多分配一个空间,以存放终止符

	int dSize=mbstowcs(dBuf, sBuf, 0)+1;


	dBuf=new wchar_t[dSize];

	wmemset(dBuf, 0, dSize);

 
	int nRet=mbstowcs(dBuf, sBuf, sSize);

	if(nRet<=0)

	{
		printf("转换失败\n");

	}
	else
	{
		printf("转换成功%d字符\n", nRet);

		wprintf(L"%ls\n", dBuf);

	}

wcstombs() function

Function: Convert wide byte into multi-byte character string

prototype:

size_t wcstombs(

       char *mbstr,

       const wchar_t *wcstr,

       size_t count

);

Parameter Description:

  • The wcstr target cache is used to store the converted multi-byte characters; when the value is NULL, the return value is the number of wchar_t types required by the target cache;
  • mbstr is used to store wide characters to be converted;
  • count is used to specify the maximum number of bytes that can be stored in the receive buffer;

return value:

  • When the conversion is successful, and mbstr == NULL, return the size required by the target cache (the number of char types, but without terminator);
  • When the conversion is successful and mbstr != NULL, the number of bytes converted will be returned;
  • If the conversion is not successful, return (size_t)(-1);

Instructions:

Similar to mbstowcs() method

Sample code:


	wchar_t sBuf[20]={0};

	wcscpy(sBuf, L"Hello");

	size_t sSize=wcslen(sBuf);

	char * dBuf=NULL;

	int dSize=wcstombs(dBuf, sBuf, 0)+1;

	printf("需要%d Char\n", dSize);

	dBuf=new char[dSize];

	memset(dBuf, 0, dSize);

	int nRet=wcstombs(dBuf, sBuf, dSize);

	if(nRet<=0)
	{
		printf("转换失败\n");

	}
	else
	{
		printf("转换成功%d字符\n", nRet);

		printf("%s\n", dBuf);
	}

Note: The  
wide byte, that is, the wchar_t type uses Unicode encoding, which is utf-16 in Windows and utf-32 in Linux, while multi-byte may be many other encoding methods, such as utf-8, GB232.. .. Therefore, you need to specify the multi-byte encoding type to perform the normal conversion process. Set or get the function for multi-byte encoding: setlocale() 
Required header file: locale.h 
#include <locale.h>

 

 

 

ogo function

Guess you like

Origin blog.csdn.net/my_angle2016/article/details/115303168