实习随手记-宽窄字符串之间的转换

来源:互联网 发布:淘宝网上托福答案 编辑:程序博客网 时间:2024/04/27 14:35

<CHAR和WHAR的关系1.    实现CHAR和WCHAR的相互装换2.    了解TCHAR>//char    C语言标准数据类型,字符型,至于由几个字节组成通常由编译器决定,一般一个字节//WCHAR   为Unicode字符,即不论中英文,每个有两个字节组成/*TCHAR   如果当前编译方式为 ANSI(默认)方式,TCHAR等价于CHAR, 如果为Unicode方式,TCHAR等价于WCHAR。*//*LPCSTR和LPSTR   即以零结尾的字符串指针,相当于CHAR *。 LPSTR、LPCSTR相当于char *,所以这种类型变量的赋值等同于char *的赋值。Ex1: LPSTR lpstrMsg = "I'm tired.";Ex2:   char strMsg[]="I'm tired.";LPSTR lpstrMsg = (LPSTR) strMsg;*/#include<stdio.h>#include<windows.h>#include<tchar.h>int WINAPI WinMain(_In_  HINSTANCE hInstance,_In_  HINSTANCE hPrevInstance,_In_  LPSTR lpCmdLine,_In_  int nCmdShow){//==========================================================================================================char sText[20] = { "我是窄字符串!" };       //定义一个GB2312的字串DWORD dcNum = MultiByteToWideChar(CP_ACP, 0, sText, -1, NULL, 0);//计算这个GB2312实际有几个字节组成wchar_t* pwText;pwText = new wchar_t[dcNum];if (!pwText){delete[]pwText;}MultiByteToWideChar(CP_ACP, 0, sText, -1, pwText, dcNum);//把GB2312变成UNICODE:sText  ->  pwTextMessageBox(NULL, pwText,L"chartowchar",MB_OK);//delete[]pwText;//释放内存wchar_t wText[] = { L"我是宽字符" };dwNum= WideCharToMultiByte(CP_OEMCP, NULL, wText, -1, NULL, 0, NULL, FALSE);//计算这个UTF8实际有几个字节组成char* psText = new char[dwNum];if(!psText){delete[]psText;}WideCharToMultiByte(CP_OEMCP, NULL, wText, -1, psText, dwNum, NULL, FALSE);delete[]psText;return 0;}封装函数形式:wchar_t * convertCharToWChar_t(char * data){size_t len = strlen(data) + 1;size_t converted = 0;wchar_t *wstr;wstr = (wchar_t*)malloc(len * sizeof(wchar_t));mbstowcs_s(&converted, wstr, len, data, _TRUNCATE);return wstr;}char * convertWChar_tToChar(wchar_t * data){size_t len = wcslen(data) + 1;size_t converted = 0;char * cstr;cstr = (char*)malloc(len * sizeof(char));wcstombs_s(&converted, cstr, len, data, _TRUNCATE);return cstr;}// 宽 字符转UTF8string EncodeUtf8(wstring in){string s(in.length() * 3 + 1, ' ');size_t len = ::WideCharToMultiByte(CP_UTF8, 0,in.c_str(), in.length(),&s[0], s.length(),NULL, NULL);s.resize(len);return s;}// UTF8 转宽字符wstring DecodeUtf8(string in){wstring s(in.length(), _T(' '));size_t len = ::MultiByteToWideChar(CP_UTF8, 0,in.c_str(), in.length(),&s[0], s.length());s.resize(len);return s;}
std::wstring Ansi2WChar(LPCSTR pszSrc){int nSize = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pszSrc, -1, 0, 0);//获取需要开辟的宽字符空间大小if (nSize <= 0) return NULL;WCHAR *pwszDst = new WCHAR[nSize + 1];//开辟空间(记得之后要释放),+1是为了设置字符串末尾的结束符memset(pwszDst, 0, (nSize + 1) * sizeof(WCHAR));MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pszSrc, -1, pwszDst, nSize);pwszDst[nSize] = 0;//设置字符串末尾的结束符if (pwszDst[0] == 0xFEFF) // skip Oxfeff  {for (int i = 0; i < nSize; i++)pwszDst[i] = pwszDst[i + 1];}wstring wcharString(pwszDst);//将自己开辟空间中的字符串做拷贝,为了接下来的释放动作delete pwszDst; pwszDst = NULL; //释放自己new所开辟的堆空间,return wcharString;}
//string到wstringstd::wstring s2ws(const string& s){return Ansi2WChar(s.c_str());}std::string WChar2Ansi(LPCWSTR pwszSrc){int nLen = WideCharToMultiByte(CP_ACP, 0, pwszSrc, -1, NULL, 0, NULL, NULL);if (nLen <= 0) return std::string("");char* pszDst = new char[nLen+1];memset(pszDst, 0, (nLen+1) * sizeof(char));if (NULL == pszDst) return std::string("");WideCharToMultiByte(CP_ACP, 0, pwszSrc, -1, pszDst, nLen, NULL, NULL);pszDst[nLen] = 0;std::string strTemp(pszDst);delete pszDst; pszDst = NULL;return strTemp;}//wstring到stringstring ws2s(wstring inputws){return WChar2Ansi(inputws.c_str());}


原创粉丝点击