Trap D | Gavrinis a écrit :
Bonjour,
Je dois récupérer des données encodées en UTF8 dans une base MySQL. Le problème c'est que les accents ne passent pas . Je cherche une façon de modifier le charset de mon programme C ou une fonction codant/décodant l'unicode mais je n'ai rien trouvé.
Comment puis-je faire pour afficher mes chaines UTF8 accentuées ?
|
Tu peux essayer ces deux codes qui font les conversion ansi -> utf8 et utf8 -> ansi et qui fonctionnent bien.
Code :
- char* AnsiToUtf8( const char * ansi, int len )
- {
- char *ansistr = NULL;
- int length = MultiByteToWideChar(CP_ACP, 0, ansi, len, NULL, NULL );
- WCHAR *lpszW = NULL;
- lpszW = new WCHAR[length+1];
- MultiByteToWideChar(CP_ACP, 0, ansi, -1, lpszW, length );
- int length1 = WideCharToMultiByte(CP_UTF8, 0, lpszW, length, NULL, 0, NULL, NULL);
- ansistr = ( char * ) calloc ( sizeof(char), length1+1 );
- WideCharToMultiByte(CP_UTF8, 0, lpszW, length, ansistr, length1, NULL, NULL);
- ansistr[length1] = 0;
- delete[] lpszW;
- return ansistr;
- }
- char *Utf8ToAnsi(const char * utf8, int len)
- {
- char *utf8str = NULL;
- int length = MultiByteToWideChar(CP_UTF8, 0, utf8, len, NULL, NULL );
- WCHAR *lpszW = NULL;
- lpszW = new WCHAR[length+1];
- MultiByteToWideChar(CP_UTF8, 0, utf8, -1, lpszW, length );
- int length1 = WideCharToMultiByte(CP_ACP, 0, lpszW, length, NULL, 0, NULL, NULL);
- utf8str = ( char * ) calloc ( sizeof(char), length1+1 );
- WideCharToMultiByte(CP_ACP, 0, lpszW, length, utf8str, length1, NULL, NULL);
- utf8str[length1] = 0;
- delete[] lpszW;
- return utf8str;
- }
|
Message édité par Trap D le 17-12-2008 à 13:58:56
|