ASCII编码和UNICODE编码转换

来源:互联网 发布:java中final修饰的类 编辑:程序博客网 时间:2024/05/18 02:15
#include <stdio.h>#include <windows.h>#include <vector>#include <iostream>#include <string>using namespace std;//string转wstringinline void Ascii2WideString( const std::string& src, std::wstring& dest );//wstring转stringinline 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 );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转wstringvoid Ascii2WideString(const std::string& src, std::wstring& dest){int nLength = MultiByteToWideChar( CP_ACP, 0, src.c_str(), -1, NULL, NULL );dest.resize(nLength);LPWSTR lpwszStr = new wchar_t[nLength];MultiByteToWideChar( CP_ACP, 0, src.c_str(), -1, lpwszStr, nLength );dest = lpwszStr;delete [] lpwszStr;return;}//wstring转stringvoid WideString2Ascii(const std::wstring& src,std::string& dest){int nLength = WideCharToMultiByte( CP_ACP, 0, src.c_str(), -1, NULL, NULL,NULL,NULL );dest.resize(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;}

0 0
原创粉丝点击