conversion ISO-8859-1 vers UTF-8 avec iconv
Tuesday, 22 February 2011
|
Après m'être battu un moment avec iconv, voici un petit exemple de conversion ISO-8859-1 vers UTF-8 :
#include ‹iconv.h› int translate_text(char** text, char free_text) { char* tmp_in, *tmp_out, *original_out; size_t mult_size, size_in, size_out; if (!text || !*text) return 1; converter = iconv_open ("UTF-8", "ISO-8859-1"); if (converter <= 0) return 1; size_in = strlen(*text); mult_size = size_out = size_in*4; // Because UTF8 is bigger thant ISO-XXX tmp_in = *text; original_out = tmp_out = malloc(size_out); iconv(converter, &tmp_in, &size_in, &tmp_out, &size_out); // All variables are modified by iconv original_out[mult_size-size_out] = 0; if (free_text) free(*text); *text = original_out; iconv_close(converter); return 0; }