GBK utf8 ANSI Unicode Conversion

Source: Internet
Author: User

1. Convert GBK to utf8

String gbktoutf8 (const STD: string & strgbk)
{
String stroututf8 = "";
Wchar * str1;
Int n = multibytetowidechar (cp_acp, 0, strgbk. c_str (),-1, null, 0 );
Str1 = new wchar [N];
Multibytetowidechar (cp_acp, 0, strgbk. c_str (),-1, str1, N );
N = widechartomultibyte (cp_utf8, 0, str1,-1, null, 0, null, null );
Char * str2 = new char [N];
Widechartomultibyte (cp_utf8, 0, str1,-1, str2, N, null, null );
Stroututf8 = str2;
Delete [] str1;
Str1 = NULL;
Delete [] str2;
Str2 = NULL;
Return stroututf8;
}

2. Convert utf8 to GBK

String utf8togbk (const STD: string & strutf8)
{
Int Len = multibytetowidechar (cp_utf8, 0, strutf8.c _ STR (),-1, null, 0 );
Unsigned short * wszgbk = new unsigned short [Len + 1];
Memset (wszgbk, 0, Len * 2 + 2 );
Multibytetowidechar (cp_utf8, 0, (lpctstr) strutf8.c _ STR (),-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;
STD: String strtemp (szgbk );
Delete [] szgbk;
Delete [] wszgbk;
Return strtemp;

}

// Conversion from UTF-8 to gb2312
Char * u2g (const char * utf8)
{
Int Len = multibytetowidechar (cp_utf8, 0, utf8,-1, null, 0 );
Wchar_t * wstr = new wchar_t [Len + 1];
Memset (wstr, 0, Len + 1 );
Multibytetowidechar (cp_utf8, 0, utf8,-1, wstr, Len );
Len = widechartomultibyte (cp_acp, 0, wstr,-1, null, 0, null, null );
Char * STR = new char [Len + 1];
Memset (STR, 0, Len + 1 );
Widechartomultibyte (cp_acp, 0, wstr,-1, STR, Len, null, null );
If (wstr) Delete [] wstr;
Return STR;
}

// Gb2312 to UTF-8 Conversion
Char * g2u (const char * gb2312)
{
Int Len = multibytetowidechar (cp_acp, 0, gb2312,-1, null, 0 );
Wchar_t * wstr = new wchar_t [Len + 1];
Memset (wstr, 0, Len + 1 );
Multibytetowidechar (cp_acp, 0, gb2312,-1, wstr, Len );
Len = widechartomultibyte (cp_utf8, 0, wstr,-1, null, 0, null, null );
Char * STR = new char [Len + 1];
Memset (STR, 0, Len + 1 );
Widechartomultibyte (cp_utf8, 0, wstr,-1, STR, Len, null, null );
If (wstr) Delete [] wstr;
Return STR;
}
Void atow (lpstr psz, lptstr & pwsz)
{
Assert (psz! = NULL );
Const int size = multibytetowidechar (cp_acp, 0, psz,-1, null, 0 );
Assert (size> 0 );
Pwsz = (lpwstr) malloc (sizeof (wchar) * size );
Assert (pwsz! = NULL );
Memset (pwsz, 0, sizeof (wchar) * size );
Verify (multibytetowidechar (cp_acp, 0, psz,-1, pwsz, size )! = 0 );
}
Void wtoa (lpcwstr pwsz, lpstr & m_psz)
{
Assert (pwsz! = NULL );
Const int size = widechartomultibyte (cp_acp, 0, pwsz,-1,
Null, 0, null, null );
Assert (size> 0 );
M_psz = (lpstr) malloc (sizeof (char) * size );
Assert (m_psz! = NULL );
Widechartomultibyte (cp_acp, 0, pwsz,-1, m_psz, size, null, null );
}



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.