Conversion between string encodings (Gb2312<->utf8<->unicode) and urlencoding

Source: Internet
Author: User

LPWStr Gb2312tounicode (LPCSTR lpszinbuf)
{
if (NULL = = lpszinbuf)
{
return NULL;
}
wchar* pBuf = NULL;
int nlen = MultiByteToWideChar (CP_ACP, 0, Lpszinbuf,-1, pBuf, 0);
PBuf = new Wchar[nlen];
memset (pBuf, 0, sizeof (WCHAR) * nlen);
MultiByteToWideChar (CP_ACP, 0, Lpszinbuf,-1, PBuf, Nlen);
return pBuf;
}

LPSTR UnicodeToGB2312 (LPCWSTR lpszinbuf)
{
if (NULL = = lpszinbuf)
{
return NULL;
}
char* pBuf = NULL;
int nlen = WideCharToMultiByte (CP_ACP, 0, Lpszinbuf,-1, pBuf, 0, NULL, NULL);
PBuf = new Char[nlen];
memset (pBuf, 0, sizeof (CHAR) * nlen);
WideCharToMultiByte (CP_ACP, 0, Lpszinbuf,-1, PBuf, nlen, NULL, NULL);
return pBuf;
}

LPWStr Utf8tounicode (LPCSTR lpszinbuf)
{
if (NULL = = lpszinbuf)
{
return NULL;
}
wchar* pBuf = NULL;
int nlen = MultiByteToWideChar (Cp_utf8, 0, Lpszinbuf,-1, pBuf, 0);
PBuf = new Wchar[nlen];
memset (pBuf, 0, sizeof (WCHAR) * nlen);
MultiByteToWideChar (Cp_utf8, 0, Lpszinbuf,-1, PBuf, Nlen);
return pBuf;
}

LPSTR UnicodeToUTF8 (LPCWSTR lpszinbuf)
{
if (NULL = = lpszinbuf)
{
return NULL;
}
char* PBuf =null;
int nlen = WideCharToMultiByte (Cp_utf8, 0, Lpszinbuf,-1, pBuf, 0, NULL, NULL);
PBuf = new Char[nlen];
memset (pBuf, 0, sizeof (CHAR) * nlen);
WideCharToMultiByte (Cp_utf8, 0, Lpszinbuf,-1, PBuf, nlen, NULL, NULL);
return pBuf;
}

LPSTR Gb2312toutf8 (LPCSTR lpszinbuf)
{
if (NULL = = lpszinbuf)
{
return NULL;
}
LPWStr lpsztmp = Gb2312tounicode (LPSZINBUF);
LPSTR lpszbuf = UnicodeToUTF8 (lpsztmp);
Delete[] lpsztmp;
Lpsztmp = NULL;
return lpszbuf;
}

LPSTR utf8togb2312 (LPCSTR lpszinbuf)
{
if (NULL = = lpszinbuf)
{
return NULL;
}
LPWStr lpsztmp = Utf8tounicode (LPSZINBUF);
LPSTR lpszbuf = UnicodeToGB2312 (lpsztmp);
Delete[] lpsztmp;
Lpsztmp = NULL;
return lpszbuf;
}

LPSTR gb2312encoding (LPCSTR lpszinbuf)
{
if (NULL = = lpszinbuf)
{
return NULL;
}
const int nlen = strlen (lpszinbuf) * 3 + 1;
char* PBuf = new Char[nlen];
memset (pBuf, 0, sizeof (CHAR) * nlen);
CHAR Sztmp[4] = {0};
for (int i = 0; I! = strlen (lpszinbuf); ++i)
{
if (Isdbcsleadbyte (Lpszinbuf[i]))
{
sprintf_s (Sztmp, _countof (sztmp), "%%%02x", Lpszinbuf[i] & 0xFF);
}
Else
{
sprintf_s (Sztmp, _countof (sztmp), "%c", Lpszinbuf[i]);
}
sprintf_s (PBuf + strlen (pBuf), Nlen-strlen (PBuf), "%s", sztmp);
}
return pBuf;
}

LPSTR utf8encoding (LPCSTR lpszinbuf)
{
if (NULL = = lpszinbuf)
{
return NULL;
}
const int nlen = strlen (lpszinbuf) * 3 + 1;
char* PBuf = new Char[nlen];
memset (pBuf, 0, sizeof (CHAR) * nlen);
CHAR Sztmp[4] = {0};
for (int i = 0; I! = strlen (lpszinbuf); ++i)
{
if (Isdbcsleadbyte (Lpszinbuf[i]))
{
sprintf_s (Sztmp, _countof (sztmp), "%%%02x", Lpszinbuf[i] & 0xFF);
}
Else
{
sprintf_s (Sztmp, _countof (sztmp), "%c", Lpszinbuf[i]);
}
sprintf_s (PBuf + strlen (pBuf), Nlen-strlen (PBuf), "%s", sztmp);
}
return pBuf;
}

Test code
#include "stdafx.h"
#include <Windows.h>
#include <strsafe.h>

int _tmain (int argc, _tchar* argv[])
{
CHAR sztext[] = "Baidu hi";
char* PBuf = gb2312encoding (Sztext);
MessageBoxA (NULL, PBUF, NULL, 0);
Delete[] PBuf;
PBuf = NULL;
return 0;
}

Conversion between string encodings (Gb2312<->utf8<->unicode) and urlencoding

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.