char 与 wchar_t 转换

//将wchar_t* 转成char*
int w2c( const wchar_t *pwc_str, char *pc_outstr, int i_coutlen )
{
	if ( pwc_str )
	{
		int i_wclen = wcslen( pwc_str );

		//获取转换后的长度
		int i_reallen = WideCharToMultiByte( 0, // specify the code page used to perform the conversion
		0,         // no special flags to handle unmapped characters
		pwc_str,     // wide character string to convert
		i_wclen,   // the number of wide characters in that string
		NULL,      // no output buffer given, we just want to know how long it needs to be
		0,
		NULL,      // no replacement character given
		NULL );    // we don't want to know if a character didn't make it through the translation

		// make sure the buffer is big enough for this, making it larger if necessary
		if( i_reallen >= i_coutlen )
			i_reallen = i_coutlen - 1;

		// transform unicode to ascii
		WideCharToMultiByte( 0, // specify the code page used to perform the conversion
		0,         // no special flags to handle unmapped characters
		pwc_str,   // wide character string to convert
		i_wclen,   // the number of wide characters in that string
		pc_outstr, // put the output ascii characters at the end of the buffer
		i_reallen,                           // there is at least this much space there
		NULL,      // no replacement character given
		NULL );
		pc_outstr[i_reallen] = 0;
		return 0;
	}
	return -1;
}

//将char* 转成wchar_t*
int c2w( const char *pc_str, wchar_t *pwc_outstr, int i_wcoutlen )
{
	if( pc_str )
	{
		int i_clen = strlen( pc_str );
		
		//获取转换后的长度
		int i_reallen = MultiByteToWideChar( CP_ACP, 0, pc_str,(int)i_clen, NULL, 0 );
		if( i_reallen >= i_wcoutlen )
			i_reallen = i_wcoutlen - 1;
		MultiByteToWideChar( CP_ACP, 0, pc_str, i_clen, pwc_outstr, i_reallen );
		pwc_outstr[i_reallen] = 0;
		return 0;
	}
	return -1;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值