多字节与unicode之间的转换

两者之间相互转换有2套函数可以利用

c运行时库:         wcstombs   mbstowcs 

 

windows API:  MultiByteToWideChar  WideCharToMultiByte

 

函数不用解释, 看字面意思就可以. 下面列出使用案例:

 

c运行时库:

#include <stdio.h>
#include <stdlib.h>

void main( void )
{
  int      i;
  char    *pmbbuf   = (char *)malloc( MB_CUR_MAX );
  wchar_t *pwchello = L”Hello, world.”;

  printf( “Convert wide-character string:/n” );
  i = wcstombs( pmbbuf, pwchello, MB_CUR_MAX );
  printf( “/tCharacters converted: %u/n”, i );
  printf( “/tMultibyte character: %s/n/n”, pmbbuf );
}

windows API:

 

 WCHAR wShortcut[MAX_PATH];
 int n = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, pFileName, -1,
  wShortcut, MAX_PATH);

 

 char szName[MAX_FILTER_NAME];
 int cch = WideCharToMultiByte(CP_ACP, 0, FilterInfo.achName,
  MAX_FILTER_NAME, szName, MAX_FILTER_NAME, 0, 0);

 

 

另外c运行时库提供了一套多字节和unicode之间的字符转换. mbtowc, wctomb

版权所有,禁止转载. 如需转载,请先征得博主的同意,并且表明文章出处,否则按侵权处理.

    分享到:

Leave a Reply

Your email address will not be published. Required fields are marked *