WideCharToMultiByte和MultiByteToWideChar

两个函数定义:
 
int MultiByteToWideChar(
  UINT CodePage,
  DWORD dwFlags,
  LPCSTR lpMultiByteStr,
  int cbMultiByte,
  LPWSTR lpWideCharStr,
  int cchWideChar
);
 
int WideCharToMultiByte(
  UINT CodePage,
  DWORD dwFlags,
  LPCWSTR lpWideCharStr,
  int cchWideChar,
  LPSTR lpMultiByteStr,
  int cbMultiByte,
  LPCSTR lpDefaultChar,
  LPBOOL lpUsedDefaultChar
);
 
 
  
  CodePage
 
 
 
 

[in] Code page to be used to perform the conversion. This parameter can be given the value of any code page that is installed or available in the system.

The following table shows possible values.

ValueDescription

CP_ACP

ANSI code page

CP_MACCP

Not supported

CP_OEMCP

OEM code page

CP_SYMBOL

Not supported

CP_THREAD_ACP

Not supported

CP_UTF7

UTF-7 code page

CP_UTF8

UTF-8 code page

/
MultiByteToWideChar

lpMultiByteStr

[in] Pointer to the character string to be converted.

cbMultiByte

[in] Size, in bytes, of the string pointed to by the lpMultiByteStr parameter. If this value is –1, the string is assumed to be null terminated and the length is calculated automatically.

lpWideCharStr

[out] Pointer to a buffer that receives the translated string.

cchWideChar

[in] Size, in wide characters, of the buffer pointed to by the lpWideCharStr parameter.

If this value is zero, the function returns the required buffer size, in wide characters, and makes no use of the lpWideCharStr buffer

WideCharToMultiByte
lpWideCharStr

[in] Pointer to the wide-character string to be converted.

cchWideChar

[in] Number of Unicode (16-bit) characters in the string pointed to by the lpWideCharStr parameter. If this value is –1, the string is assumed to be null-terminated and the length is calculated automatically.

lpMultiByteStr

[out] Pointer to the buffer to receive the translated string.

cbMultiByte

[in] Size, in bytes, of the buffer pointed to by the lpMultiByteStr parameter. If this value is zero, the function returns the number of bytes required for the buffer. (In this case, the lpMultiByteStr buffer is not used.)

lpDefaultChar

[in] Pointer to the character used if a wide character cannot be represented in the specified code page. If this parameter is NULL, a system default value is used.

The function is faster when both lpDefaultChar and lpUsedDefaultChar are NULL.

lpUsedDefaultChar

[in, out] Pointer to a flag that indicates whether a default character was used.

The flag is set to TRUE if one or more wide characters in the source string cannot be represented in the specified code page. Otherwise, the flag is set to FALSE.

This parameter can be NULL.

另外,自己写了一段常用的转换代码
#include<iostream>
#include <Windows.h>
#include<string>
using namespace std;

wstring ANSIToUnicode( const string& str )
{
	int  unicodeLen = ::MultiByteToWideChar(CP_ACP,0,str.c_str(),-1,NULL,0);   
	wchar_t *pUnicode = new  wchar_t[unicodeLen+1];  
	MultiByteToWideChar(CP_ACP,0,str.c_str(),-1,(LPWSTR)pUnicode,unicodeLen);
	pUnicode[unicodeLen] = '\0';
	wstring ret = ( wchar_t* )pUnicode;
	delete  pUnicode; 
	return  ret;  
}
string UnicodeToANSI( const wstring& str )
{
	// wide char to multi char
	int ANSILen = WideCharToMultiByte( CP_ACP,0,str.c_str(),-1,NULL,0,NULL,NULL);
	char* pANSI = new char[ANSILen + 1];
	WideCharToMultiByte( CP_ACP,0,str.c_str(),-1,pANSI,ANSILen,NULL,NULL);
	pANSI[ANSILen] = '\0';
	string strret = pANSI;
	delete[] pANSI;
	return strret;
}
wstring UTF8ToUnicode( const string& str )
{
	int  unicodeLen = MultiByteToWideChar(CP_UTF8,0,str.c_str(),-1,NULL,0);  
	wchar_t * pUnicode = new  wchar_t[unicodeLen+1];  
	MultiByteToWideChar(CP_UTF8,0,str.c_str(),-1,(LPWSTR)pUnicode,unicodeLen);  
	pUnicode[unicodeLen] = '\0';
	wstring  ret = ( wchar_t* )pUnicode;  
	delete  pUnicode;
	return  ret;  
}
int main(void)
{
	string str1 = "中国";
	wstring wstr1 = ANSIToUnicode(str1);
	wcout.imbue(locale("chs"));  
	wcout<<wstr1<<endl;
	wstring wstr2(L"中国");
	string  str2= UnicodeToANSI(wstr2);
	cout<<str2<<endl;
	return 0;
}



                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
WideCharToMultiByteMultiByteToWideChar是Windows API中用于Unicode和多字节字符之间转换的函数。 举个例子,如果我们想要将一个Unicode字符串转换为多字节字符,可以使用WideCharToMultiByte函数。以下是一个示例代码: ```c++ // Unicode字符串 wchar_t unicodeStr[] = L"这是一个Unicode字符串"; // 转换为多字节字符 char mbStr[100]; WideCharToMultiByte(CP_UTF8, 0, unicodeStr, -1, mbStr, 100, NULL, NULL); // 输出结果 printf("多字节字符串:%s\n", mbStr); ``` 在上面的代码中,我们使用了WideCharToMultiByte函数将unicodeStr转换为多字节字符,并将结果存储在mbStr中。CP_UTF8参数指定了编码方式为UTF-8,-1参数表示转换整个字符串,100参数表示缓冲区大小为100。最后输出转换后的多字节字符。 同理,如果我们想要将一个多字节字符转换为Unicode字符串,可以使用MultiByteToWideChar函数。以下是一个示例代码: ```c++ // 多字节字符串 char mbStr[] = "这是一个多字节字符串"; // 转换为Unicode字符串 wchar_t unicodeStr[100]; MultiByteToWideChar(CP_UTF8, 0, mbStr, -1, unicodeStr, 100); // 输出结果 wprintf(L"Unicode字符串:%s\n", unicodeStr); ``` 在上面的代码中,我们使用了MultiByteToWideChar函数将mbStr转换为Unicode字符串,并将结果存储在unicodeStr中。CP_UTF8参数指定了编码方式为UTF-8,-1参数表示转换整个字符串,100参数表示缓冲区大小为100。最后输出转换后的Unicode字符串。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值