-
-
Save bambocher/7033914 to your computer and use it in GitHub Desktop.
Пример использования библиотеки iconv. Компилируется в cygwin командой: gcc charset_convert.c -liconv -o charset_convert.exe
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include <string.h> | |
#include <errno.h> | |
#include <iconv.h> | |
char *charset_convert(const char *string, char *fromcode, char *tocode) | |
{ | |
iconv_t cd; | |
size_t in, out, len, err; | |
char *dest, *outp, *inp = (char *) string; | |
cd = iconv_open(tocode, fromcode); | |
if (cd < 0) | |
return NULL; | |
in = strlen(string); | |
/* Chinese characters in GB18030 are commonly 2 bytes, in UTF-8 they are | |
* 3 bytes or more, it quite rare to find a character exceeds such range, | |
* plus we may have some non-Chinese characters which only need 1 byte, | |
* so the size allocated here should be sufficient for most cases */ | |
out = len = in * 3 / 2 + 1; | |
outp = dest = (char *) malloc(len); | |
again: | |
err = iconv(cd, &inp, &in, &outp, &out); | |
/* If the pre-allocated output buffer is not large enough, we have to | |
* enlarge it by realloc(), then update related pointers and counters */ | |
if (err == (size_t)(-1) && errno == E2BIG) | |
{ | |
size_t used = outp - dest; | |
len *= 2; /* double the size may not be the most economic option though */ | |
char *newdest = (char *) realloc(dest, len); | |
if (! newdest) | |
goto out; | |
dest = newdest; | |
outp = dest + used; | |
out = len - used - 1; | |
goto again; | |
} | |
out: | |
/* Make sure we have the trailing '\0' */ | |
if (outp) | |
*outp = '\0'; | |
iconv_close(cd); | |
return dest; | |
} | |
int main(void) | |
{ | |
printf(charset_convert("Строка в CP1251", "CP1251", "UTF-8")); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment