记录一下最近用到的几个知识点

<!-- [if gte mso 9]><xml> <w:WordDocument> <w:View>Normal</w:View> <w:Zoom>0</w:Zoom> <w:PunctuationKerning /> <w:DrawingGridVerticalSpacing>7.8 磅</w:DrawingGridVerticalSpacing> <w:DisplayHorizontalDrawingGridEvery>0</w:DisplayHorizontalDrawingGridEvery> <w:DisplayVerticalDrawingGridEvery>2</w:DisplayVerticalDrawingGridEvery> <w:ValidateAgainstSchemas /> <w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid> <w:IgnoreMixedContent>false</w:IgnoreMixedContent> <w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText> <w:Compatibility> <w:SpaceForUL /> <w:BalanceSingleByteDoubleByteWidth /> <w:DoNotLeaveBackslashAlone /> <w:ULTrailSpace /> <w:DoNotExpandShiftReturn /> <w:AdjustLineHeightInTable /> <w:BreakWrappedTables /> <w:SnapToGridInCell /> <w:WrapTextWithPunct /> <w:UseAsianBreakRules /> <w:DontGrowAutofit /> <w:UseFELayout /> </w:Compatibility> <w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel> </w:WordDocument> </xml><![endif]--><!-- [if gte mso 9]><xml> <w:LatentStyles DefLockedState="false" LatentStyleCount="156"> </w:LatentStyles> </xml><![endif]-->

1 char* 转换为wchar_t

//char* to wchar_t wchar_t wfilename[256] ={0}; char filename[] = {"c:\\init.properties"}; ulBytes = MultiByteToWideChar(CP_ACP,0,filename,-1,NULL,0); ulBytes = MultiByteToWideChar(CP_ACP,0,filename,-1,wfilename,ulBytes);

2 wchar_t 转换为char*

//wchar_t to char* ulBytes = WideCharToMultiByte(CP_ACP,0, wfilename,-1,NULL,0,NULL,NULL); ulBytes = WideCharToMultiByte(CP_ACP,0, wfilename,-1, filename,ulBytes,NULL,NULL);

3 unicode to utf-8

int UniToUTF8(CString strUnicode,char *szUtf8) { //MessageBox(strUnicode); int ilen = WideCharToMultiByte(CP_UTF8, 0, (LPCTSTR)strUnicode, -1, NULL, 0, NULL, NULL); char *szUtf8Temp=new char[ilen + 1]; memset(szUtf8Temp, 0, ilen +1); WideCharToMultiByte (CP_UTF8, 0, (LPCTSTR)strUnicode, -1, szUtf8Temp, ilen, NULL,NULL); //size_t a = strlen(szUtf8Temp); sprintf(szUtf8, "%s", szUtf8Temp);// delete[] szUtf8Temp; return ilen; }

4. GBK to utf-8

void ConvertGBKToUtf8(CString& strGBK) { int len=MultiByteToWideChar(CP_ACP, 0, (LPCTSTR)strGBK, -1, NULL,0); unsigned short * wszUtf8 = new unsigned short[len+1]; memset(wszUtf8, 0, len * 2 + 2); MultiByteToWideChar(CP_ACP, 0, (LPCTSTR)strGBK, -1, wszUtf8, len); len = WideCharToMultiByte(CP_UTF8, 0, wszUtf8, -1, NULL, 0, NULL, NULL); char *szUtf8=new char[len + 1]; memset(szUtf8, 0, len + 1); WideCharToMultiByte (CP_UTF8, 0, wszUtf8, -1, szUtf8, len, NULL,NULL); strGBK = szUtf8; delete[] szUtf8; delete[] wszUtf8; }

5. utf-8 to GBK

void ConvertUtf8ToGBK(CString& strUtf8) { int len=MultiByteToWideChar(CP_UTF8, 0, (LPCTSTR)strUtf8, -1, NULL,0); unsigned short * wszGBK = new unsigned short[len+1]; memset(wszGBK, 0, len * 2 + 2); MultiByteToWideChar(CP_UTF8, 0, (LPCTSTR)strUtf8, -1, wszGBK, len); len = WideCharToMultiByte(CP_ACP, 0, wszGBK, -1, NULL, 0, NULL, NULL); char *szGBK=new char[len + 1]; memset(szGBK, 0, len + 1); WideCharToMultiByte (CP_ACP, 0, wszGBK, -1, szGBK, len, NULL,NULL); strUtf8 = szGBK; delete[] szGBK; delete[] wszGBK; }

6. 读写unicode 文件。

wchar_t* ReadUFile(wchar_t* path) { CStdioFileEx fileEx; fileEx.SetCodePage(CP_UTF8); CString sText, sLine; if(fileEx.Open(path, CFile::modeRead | CFile::typeText)) { // Read first 15 lines for (short nLineCount = 0; nLineCount < 16 && fileEx.ReadString(sLine); nLineCount++) { sText += sLine + sNEWLINE; } fileEx.Close(); } return sResult.GetBuffer(sResult.GetLength()); } bool WriteUFile(wchar_t* path,wchar_t* content) { CStdioFileEx fileEx; fileEx.SetCodePage(CP_UTF8); CString sText, sLine; UINT nWriteFlags = CFile::modeCreate | CFile::modeWrite | CFile::typeText; //nWriteFlags |= CStdioFileEx::modeWriteUnicode; if(fileEx.Open(path, nWriteFlags)) { CString scontent = content; fileEx.WriteString(scontent); fileEx.Close(); return true; } else { return false; } }

这里用到了 CStdioFileEx ,下载地址: http://files.cnblogs.com/tingsking/StdioFileEx.zip

7. 发送http 请求

void SendHttp() { CString strData =”cardno=123456&address=张三”; ConvertGBKToUtf8(strData); try { DWORD dwServiceType = AFX_INET_SERVICE_HTTP; CString szServer, szObject; INTERNET_PORT nPort; CString url; GetDlgItemText(IDC_EDIT_ADDRESS,url); AfxParseURL(url, dwServiceType, szServer, szObject, nPort); CInternetSession Session ; CHttpConnection *pHttpConnect = Session.GetHttpConnection(szServer,INTERNET_FLAG_KEEP_CONNECTION,nPort,NULL,NULL) ; if( pHttpConnect ) { CHttpFile* pFile = pHttpConnect->OpenRequest( CHttpConnection::HTTP_VERB_POST, szObject); if ( pFile ) { CString strHead = _T("Content-Type: application/x-www-form-urlencoded") ; pFile->AddRequestHeaders("Accept: */*"); pFile->AddRequestHeaders("Accept-Language: zh-cn"); pFile->AddRequestHeaders("Accept-Encoding: gzip"); pFile->AddRequestHeaders("Accept-Charset: utf-8"); ret = pFile->SendRequest( strHead ,(LPVOID)(LPCTSTR)strData ,strData.GetLength() ); pFile->Close(); delete pFile ; } pHttpConnect->Close() ; delete pHttpConnect ; } } catch( CInternetException *e ) { e->Delete(); } }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值