[xml] How to use iconv library to convert a UTF-8 string to GB2312 string
- From: "Li Baoli" <libaoli btamail net cn>
- To: <xml gnome org>
- Subject: [xml] How to use iconv library to convert a UTF-8 string to GB2312 string
- Date: Fri, 27 Dec 2002 21:48:39 +0800
Dear All,
I'd like to develop a program dealing with XML documents which contain
Chinese text encoded in GB2312. I used the libxml2 library. Unfortunately, I
encountered problems while converting a UTF-8 string to GB2312 string using
iconv library. Below is the codes I wrote. Would you please give me some
hints about how to solve this problem? Thanks very much!
======================================================
/*function invocation:*/
fprintf(stdout, "The converted result is %s\n",
codesetswitch("UTF-8","GB2312", UTF_8_String));
------------------------------------------
/*function declaration:*/
char *strcodechange(const char *from_code, const char *to_code, unsigned
char *instr)
{
iconv_t cd;
char from[BUFSIZ], to[BUFSIZ];
// char *from_code, *to_code;
char *tptr;
const char *fptr;
size_t ileft, oleft, ret;
cd = iconv_open((const char *)to_code, (const char *)from_code);
strcpy(from, (const char *)instr);
memset(to,0,BUFSIZ);
if (cd == (iconv_t)-1)
{
/** iconv_open failed*/
(void) fprintf(stderr, "iconv_open(%s, %s) failed\\n", to_code,
from_code);
return NULL;
}
fptr = from;
tptr = to;
oleft = BUFSIZ;
ret = iconv(cd, &fptr, &ileft, &tptr, &oleft);
if (ret != (size_t)-1)
{
/** iconv succeeded*/
(void) fprintf(stdout,"string:%s ; result: %s\n", from, to);
}
else
{
(void)fprintf(stderr, "iconv error!\n");
return NULL;
}
(void) iconv_close(cd);
return to;
}
---------------
/*OutPut*/
iconv error!
==================================================================
Best regards,
Li Baoli
--------------------------------------------------
Li Baoli
Institute of Computational Linguistics
Department of Computer Science and Technology
Peking University
Beijing, 100871 Phone: 86-10-6276 5835 ext 203
P.R. China Email: libaoli btamail net cn
--------------------------------------------------
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]