Required when using Sqlite3
Usage:
Char * src = "..."; // ANSI or UTF8 string to be converted
Char * dst = NULL; // Save the memory pointer internally allocated by the function.
Convert to UTF-8: to_utf8 (src, & dst );
Convert to ANSI: to_gb (src, & dst );
Returned value: Zero-failed, non-zero-successful.
Note: If the operation is successful, you need to manually release the space allocated within the function:
Copy codeThe Code is as follows:
If (dst)
{
Free (dst );
Dst = NULL;
}
Code:
Copy codeThe Code is 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: girls do not cry