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