Provides an ANSI to utf8 Conversion Function for sqlite3

Source: Internet
Author: User

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

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.