Unicode与char *转换 - WideCharToMultiByte/MultiByteToWideChar

本文介绍了如何使用WideCharToMultiByte和MultiByteToWideChar函数实现Unicode与ANSI编码之间的相互转换。通过两个示例代码展示了从宽字节到多字节(如从Unicode到char*)以及从多字节到宽字节(如从char*到Unicode)的具体步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

WideCharToMultiByte:Unicode转char *(宽字节转多字节)
MSDN:[url]http://msdn.microsoft.com/en-us/library/dd374130(VS.85).aspx[/url]

#include "stdafx.h"
#include "stdio.h"
#include "windows.h"

int _tmain(int argc, _TCHAR* argv[])
{
TCHAR strWideChar[] = _T("zerosoul"); //Unicode宽字节编码字符串strWideChar
//获得转换后的字符串长度n_len
int n_len = WideCharToMultiByte(CP_ACP,NULL,strWideChar,-1,NULL,0,NULL,NULL);
char* strMultiByte = new char[n_len]; //ANSI多字节编码字符串strMultiByte
WideCharToMultiByte(CP_ACP,NULL,strWideChar,-1,strMultiByte,n_len,NULL,NULL); //开始转换

printf("strMultiByte : %s\n",strMultiByte);
return 0;
}


MultiByteToWideChar:char *转Unicode(多字节转宽字节)
MSDN:[url]http://msdn.microsoft.com/en-us/library/dd319072(VS.85).aspx[/url]

#include "stdafx.h"
#include "stdio.h"
#include "windows.h"

int _tmain(int argc, _TCHAR* argv[])
{
char strMultiByte[] = "zerosoul"; //ANSI多字节编码字符串strMultiByte
//获得转换后的字符串长度n_len
int n_len = MultiByteToWideChar(CP_ACP,NULL,strMultiByte,-1,NULL,0);
TCHAR* strWideChar = new TCHAR[n_len]; //Unicode宽字节编码字符串strWideChar
MultiByteToWideChar(CP_ACP,NULL,strMultiByte,-1,strWideChar,n_len); //开始转换

_tprintf(_T("strWideChar : %s\n"),strWideChar);
return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值