C + + libxml Chinese issues

Source: Internet
Author: User
Tags sin

C + + in the use of libxml in the operation of the characters of the time there are various write errors, read garbled and other problems, after half a day of tossing, write a note:


It's easy to know what the problem is by first clarifying the following things:

1. The Chinese string constants appearing in the CPP file are saved in ANSI format, and the gb2312 format can be understood as a subset of the ANSI format. (TXT and XML) files generated in the Chinese and Japanese operating systems, although all are ANSI, however, in the Simplified Chinese system, the ANSI encoding represents GB2312 encoding, in the Japanese operating system, the ANSI code represents the JIS code. Different ANSI encodings are incompatible, and when information is exchanged internationally, text that is in two languages cannot be stored in the same piece of ANSI-encoded text. )

2, Libxml interface of all parameters input will be treated as UTF-8 encoding format, output is also.

3, if the character passed into the LIBXM interface is a non-UTF-8 encoding format, such as gb2312, if the string contains Chinese, it will be possible to make Xmlsaveformatfileenc () return 1, resulting in a save failure.

4, the acquisition of Chinese is for utf-8 format, if not converted to other formats and directly displayed, may appear garbled.


Below is the implementation of the code conversion function for Utf-8 and gb2312:

#include "iconv.h" #include <string.h>//code conversion: Converting from one encoding to another encoding int code_convert (char* from_charset, char* To_charset,  Const char* Inbuf,int Inlen, char* outbuf, int outlen) {iconv_t cd;const char** pin = &inbuf;  char** pout = &AMP;OUTBUF;CD = Iconv_open (To_charset,from_charset);  if (cd = = 0) return-1;memset (outbuf,0,outlen);  if (Iconv (CD, (const char**) pin, (unsigned int *) &inlen,pout, (unsigned int*) &outlen) = =-1) return-1;  Iconv_close (CD); return 0; }//unicode code to GB2312 code string u2g (const string sIn) {if (Sin.empty ()) {return "";} const char *INBUF = SIN.C_STR (), int noutlen = 2 * strlen (INBUF)-1;char* szout = (char*) malloc (Noutlen); if ( -1 = = Code_co Nvert ("Utf-8", "gb2312", Inbuf,strlen (INBUF), Szout,noutlen)) {free (szout); szout = NULL;}  String Sret = Szout;free (szout); return sret;} GB2312 code to Unicode code string g2u (const string sIn) {if (Sin.empty ()) {return "";} const char *INBUF = SIN.C_STR (), int noutlen = 2 * strlen (INBUF)-1;char* szout = (char*) malloc (Noutlen); if ( -1 = = CoDe_convert ("gb2312", "Utf-8", Inbuf,strlen (INBUF), Szout,noutlen)) {free (szout); szout = NULL;}   String Sret = Szout;free (szout); return sret;}

This article refer to the Web article click the Open link


C + + libxml Chinese issues

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.