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:
If(DST) {free (DST); DST=NULL ;}
Code:
# 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 ;}}
Girl don't cry (QQ: 191035066) @ 2012-12-19 21:53:24 http://www.cnblogs.com/nbsofer