Find some resources on the Internet to understand this is a coding problem, my server sent over the data is GB2312 encoded, and Ajax to receive the data as UTF-8 encoded.
Many of the online solutions are server-side based on PHP, JSP, and so on, to change the encoding of these methods are relatively simple. But my server side is a C-written CGI program, used in embedded systems, all the output is using printf.
Finally, we find a header file <iconv.h> under Linux, which realizes the conversion of various encodings. Use the following methods:
Copy Code code as follows:
int Gb2312toutf8 (char *inbuf,char *outbuf)
{
#ifdef Ischange
iconv_t cd;
int Inlen,outlen;
Inlen=strlen (INBUF)//Get the length of the input string
outlen=100;//output string length.
Char **pin=&inbuf;
Char **pout=&outbuf;
Cd=iconv_open ("Utf-8", "gb2312");/set to GB2312 to UTF-8
if (cd==0) return-1;
memset (Outbuf,0,outlen);
if (Iconv (Cd,pin,&inlen,pout,&outlen) ==-1)
return-1;
Iconv_close (CD);
#else
Strcpy (OUTBUF,INBUF);
#endif
return 0;
}
So the content to be output after conversion and then output, will not appear garbled.
However, the first page output and the subsequent Ajax request output is the same piece of code, the first output after the conversion has garbled, so I added a macro definition, in compiling the AJAX output program, added a #define Ischange 1, so that you can correctly display. (In fact, there should be a better solution, please know the friend told me.) Luoliang28@126.com)