ASCII编码和UNICODE编码转换
#include <stdio.h>
#include <windows.h>
#include <vector>
#include <iostream>
#include <string>
using namespace std;
//string转wstring
inline void Ascii2WideString( const std::string& src, std::wstring& dest );
//wstring转string
inline void WideString2Ascii(const std::wstring& dest,std::string& src);
/
/TCHAR*转char*
inline char* UnicodeToAnsi( const TCHAR* src,char* dest );
//char*转TCHAR*
inline TCHAR* AnsiToUnicode( const char* src,TCHAR* dest );
unicode编码转换二进制
int main (void)
{
char转wchar_t
//char buffer[32]={"hello"};
//TCHAR wBuf[64] = {0};
//int len=sizeof(buffer);
//int wlen=sizeof(wBuf);
/
/MultiByteToWideChar(CP_ACP, 0, (const char*)buffer, len, wBuf, wlen);
wchar_t转char
//TCHAR str1[32]={TEXT("world")};
//char  str2[64]={0};
//int len1=sizeof(str1);
//int len2=sizeof(str2);
//WideCharToMultiByte(CP_ACP,0,(const TCHAR*)str1,len2,str2,len1,NULL,NULL);
string str3="hello";
wstring str4;
Ascii2WideString(str3,str4);
wstring str5=TEXT("hello world");
string str6;
WideString2Ascii(str5,str6);
TCHAR str1[32]={TEXT("world")};
char  str2[64]={0};
UnicodeToAnsi(str1,str2);
char buffer[32]={"hello"};
TCHAR wBuf[64] = {0};
AnsiToUnicode(buffer,wBuf);
return 0;
}
//string转wstring
void Ascii2WideString(const std::string& src, std::wstring& dest)
{
{
int nLength = MultiByteToWideChar( CP_ACP, 0, src.c_str(), -1, NULL, NULL );
LPWSTR lpwszStr = new wchar_t[nLength];
MultiByteToWideChar( CP_ACP, 0, src.c_str(), -1, lpwszStr, nLength );
dest = lpwszStr;
delete [] lpwszStr;
return;
}
/
/wstring转string
void WideString2Ascii(const std::wstring& src,std::string& dest)
{
int nLength = WideCharToMultiByte( CP_ACP, 0, src.c_str(), -1, NULL, NULL,NULL,NULL ); size(nLength);
char* lpwszStr = new char[nLength];
WideCharToMultiByte( CP_ACP, 0, src.c_str(), -1, lpwszStr, nLength ,NULL,NULL);
dest = lpwszStr;
delete [] lpwszStr;
return;
}
//将wchar_t*转char*
char* UnicodeToAnsi(const wchar_t* src,char *dest)
{
int nLen = WideCharToMultiByte( CP_ACP, 0, src, -1, NULL, 0, NULL, NULL );
if (nLen == 0)
return NULL;
//char* pResult = new char[nLen];
WideCharToMultiByte( CP_ACP, 0, src, -1, dest, nLen, NULL, NULL );
return dest;
}
//char*转wchar*
TCHAR* AnsiToUnicode(const char* src,TCHAR* dest)
{
int nLen = MultiByteToWideChar( CP_ACP, MB_PRECOMPOSED, src, -1, NULL, 0 );
if (nLen == 0)
return NULL;
//wchar_t* pResult = new wchar_t[nLen];
MultiByteToWideChar( CP_ACP, MB_PRECOMPOSED, src, -1, dest, nLen );
return dest;
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。