1,wcstombs
size_t wcstombs (char* dest, const wchar_t* src, size_t max);
Convert wide-character string to multibyte string
dest
char
elements long enough to contain the resulting sequence (at most,
max bytes).
size_t is an unsigned integral type.
Return Value
The number of bytes written to dest , not including the eventual ending null-character.If a wide character that does not correspond to a valid multibyte character is encountered, a
(size_t)-1
value is returned.
Notice that size_t is an unsigned integral type, and thus none of the values possibly returned is less than zero.
Example
| |
Output:
wchar_t string: wcstombs example multibyte string: wcstombs example |
2,
mbstowcs
size_t mbstowcs (wchar_t* dest, const char* src, size_t max);
wchar_t
elements long enough to contain the resulting sequence (at most,
max wide characters).
The multibyte sequence shall begin in the initial shift state.
wchar_t
characters to write to
dest.
size_t is an unsigned integral type.
Return Value
The number of wide characters written to dest , not including the eventual terminating null character .If an invalid multibyte character is encountered, a value of
(size_t)-1
is returned.
Notice that size_t is an unsigned integral type, and thus none of the values possibly returned is less than zero.
char * sBuf = new char[30];
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);
}
http://blog.csdn.net/xiaobai1593/article/details/7063535
http://www.cplusplus.com/reference/cstdlib/mbstowcs/
//============================================================================
// Name : mingwTest.cpp
// Author :
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <iostream>
#include <tchar.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
int main(int argc, char *argv[]) {
wchar_t str[] = L"wcstombs example";
char buffer[32];
int ret;
printf ("wchar_t string: %ls \n",str);
ret = wcstombs ( buffer, str, sizeof(buffer) );
if (ret==32) buffer[31]='\0';
if (ret) printf ("ref %d multibyte string: %s \n",ret,buffer);
printf("-------------------------");
char * sBuf = new char[30];
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);
}
}