|
List Info
Thread: libxml + unicode
|
|
| libxml + unicode |

|
2006-07-26 07:40:53 |
|
Hi. You can use 'iconv' for recode a string into another encoding. For
that you must use 'iconv_open' and 'iconv_close' for open an close a
handler for recode (use man iconv_open and man iconv_close for help).
Later, for recode the string you must use function 'iconv' (man 3 iconv
for help), but, for better help, consult internet to view examples
because use directly that funcion is incorrect and won't obtain the
results what you want. I put an example here:
string Convert::convert(const string &text) const
{
iconv(_con, NULL, NULL, NULL, NULL);
// Necessary at beggining
size_t enterSize = text.size();
size_t exitSize = enterSize * 2;
// The maximum size will be the double than the sou
char *ex = (char*)malloc(exitSize * sizeof(char) + 1);
// Allocating memory
char *exit = ex;
// Aux
#ifdef __WIN32__
const char *enter = (const char*)text.c_str();
#else
char *enter = (char*)text.c_str();
#endif
memset(exit, 0, exitSize);
// Setting all bytes the value 0
size_t res = iconv(_con, &enter, &enterSize, &ex,
&exitSize); // Converting
if (res == (size_t)(-1))
// If the function 'iconv' can't convert --> error
throw(ConvertException::CONVERT);
string ret(exit, ex - exit);
free(exit);
// Freeing memory
return ret;
}
(Sorry, but I use C++, but the utilization is the same).
Where '_con' is the handler used with 'iconv_open'. That code works on
Windows and Linux.
I hope that instructions serve you.
Regards: Marcel.
Mark Wyszomiers | |