Provides an ANSI to UTF8 reciprocal function for SQLite3 _sqlite

Source: Internet
Author: User

That must be used when using Sqlite3.

How to use:

char* src = "...";//ANSI or UTF8 string to be converted
char* DST = null;//saves memory pointers allocated internally by functions and does not require incoming memory buffers

Convert to Utf-8:to_utf8 (SRC, &DST);
Convert to ANSI:TO_GB (SRC, &DST);

Return value: 0-Failed, non 0-successful.
Note: If the operation succeeds, you need to manually free the space allocated within the function:

Copy Code code as follows:

if (DST)
{
Free (DST);
DST = NULL;
}

Code:

Copy Code code as follows:

#include <windows.h>
#include <stdio.h>int To_utf8 (char* psrc, char** ppdst)
{
int Ret,ret2;
wchar_t* PWS = NULL;
char* Putf = NULL;

ret = MultiByteToWideChar (CP_ACP, 0, PSRC,-1, NULL, 0);
if (ret<=0) {
*PPDST = NULL;
return 0;
}
PWS = (wchar_t*) malloc (ret*2);
if (!PWS) {
*PPDST = NULL;
return 0;
}
MultiByteToWideChar (CP_ACP, 0, PSRC,-1, PWS, ret);
Ret2 = WideCharToMultiByte (Cp_utf8, 0, PWS,-1, NULL, 0, NULL, NULL);
if (ret2<=0) {
Free (PWS);
return 0;
}
Putf = (char*) malloc (Ret2);
if (!PUTF) {
Free (PWS);
return 0;
}
if (WideCharToMultiByte (Cp_utf8, 0, PWS, ret, PUTF, ret2, NULL, NULL)) {
*PPDST = Putf;
Free (PWS);
return 1;
}else{
Free (PWS);
Free (PUTF);
*PPDST = NULL;
return 0;
}
}

int TO_GB (char* psrc, char** ppdst)
{
int ret, Ret2;
wchar_t* PWS = NULL;
char* PGB = NULL;
ret = MultiByteToWideChar (Cp_utf8, 0, PSRC,-1, NULL, 0);
if (ret<=0) {
*PPDST = NULL;
return 0;
}
PWS = (wchar_t*) malloc (ret*2);
if (!PWS) {
*PPDST = NULL;
return 0;
}
MultiByteToWideChar (Cp_utf8, 0, PSRC,-1, PWS, ret);
Ret2 = WideCharToMultiByte (CP_ACP, 0, PWS,-1, NULL, 0, NULL, NULL);
if (ret2<=0) {
Free (PWS);
return 0;
}
PGB = (char*) malloc (Ret2);
if (!PGB) {
Free (PWS);
*PPDST = NULL;
return 0;
}
if (WideCharToMultiByte (CP_ACP, 0, PWS,-1, PGB, ret2, NULL, NULL)) {
*PPDST = PGB;
Free (PWS);
return 1;
}ELSE{*PPDST = 0;
Free (PGB);
Free (PWS);
return 0;
}
}

By: The girl does not cry

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.