C 언어 iconv 쉽게 쓰자.(UTF-8 ↔ EUC-KR)

cbcbcbcbcbcbcbccbcbcbcbcbccbcbb….

이런 기본적인 함수도 쓰기가 이렇게 힘들어서야, 원;;;

삽질로 하루는 날린 듯…

char * convert(char *tgt, char *src, char *input, float rate)
{
    iconv_t it = iconv_open(tgt, src);
    if(it == (iconv_t) -1)
    {
        fprintf(stderr, "iconv open error");
        return NULL;
    }
    size_t nSrc = strlen(input) + 1;  // for '\0'
    size_t nTgt = nSrc * rate;
    char * output = (char *)malloc(nTgt);
    char * pOutput = output;
    // printf("s:%lu\tt:%lu\n", nSrc, nTgt);

    if(iconv(it, (char **)&input, &nSrc, &pOutput, &nTgt) == (size_t) -1)
    {
        fprintf(stderr, "iconv error\n");
        return NULL;
    }
    // printf("s:%lu\tt:%lu\n", nSrc, nTgt);
    return output;    // Don't forget to 'free()'!!
}

char * u2e(char * input)
{ return convert("CP949//TRANSLIT//IGNORE", "UTF-8//TRANSLIT//IGNORE", input, 1); }

char * e2u(char * input)
{ return convert("UTF-8//TRANSLIT//IGNORE", "CP949//TRANSLIT//IGNORE", input, 3); }
Written on June 28, 2016