Char, wchar_t, utf8, Unicode, GBK Conversion

Source: Internet
Author: User

1 char * To wchar_t

View plaincopy to clipboardprint?
// Char * To wchar_t
Wchar_t wfilename [1, 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 );
// Char * To wchar_t
Wchar_t wfilename [1, 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 to char *

View plaincopy to clipboardprint?
// 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 );
// 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

View plaincopy to clipboardprint?
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;
}
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

View plaincopy to clipboardprint?
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;
}
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

View plaincopy to clipboardprint?
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;
}
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. Read and Write Unicode files.

View plaincopy to clipboardprint?
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;
}
}
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;
}
}
 

Here cstdiofileex,: http://files.cnblogs.com/tingsking/StdioFileEx.zip is used

7. Send an HTTP request

View plaincopy to clipboardprint?
Void sendhttp ()
{
Cstring strdata = "cardno = 123456 & Address = Zhang San ";

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 ();
}
}
Void sendhttp ()
{
Cstring strdata = "cardno = 123456 & Address = Zhang San ";

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 ();
}
}

 

Reprinted: http://blog.csdn.net/tingsking18/archive/2010/08/10/5801695.aspx

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.