linux没有WideCharToMultiByte,MultiByteToWideChar,换用mbstowcs,wcstombs

57 篇文章 0 订阅
1,wcstombs
size_t wcstombs (char* dest, const wchar_t* src, size_t max);

Convert wide-character string to multibyte string

dest
Pointer to an array of  char elements long enough to contain the resulting sequence (at most,  max bytes).
src
C wide string to be translated.
max
Maximum number of bytes to be written to  dest.
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/* wcstombs example */
#include <stdio.h>      /* printf */
#include <stdlib.h>     /* wcstombs, wchar_t(C) */

int main() {
  const 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 ("multibyte string: %s \n",buffer);

  return 0;
}


Output:

wchar_t string: wcstombs example 
multibyte string:  wcstombs example 
http://www.cplusplus.com/reference/cstdlib/wcstombs/


2,

mbstowcs
size_t mbstowcs (wchar_t* dest, const char* src, size_t max);

dest
Pointer to an array of  wchar_t elements long enough to contain the resulting sequence (at most,  max wide characters).
src
C-string with the multibyte characters to be interpreted.
The multibyte sequence shall begin in the initial shift state.
max
Maximum number of  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);
	  }

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值