C언어 iconv_open() Invalid argument error

rcow93의 이미지

iconv에 대해서 공부중입니다.
EUC-KR형태로 기본 설정이 되어있어서
UTF-8로 인코딩을 하고 싶은데
Invalid argument error가 도저히 해결이 안됩니다.
고수분들의 조언 부탁드리겠습니다.

int main()
{
    //iconv_t cd = iconv_open("UNICODE","UTF-8");
    iconv_t cd = iconv_open("UTF-8","EUC-KR");
    if( cd == (iconv_t)-1)
    {
        //printf("iconv_open error\n");
        perror("iconv_open error");
        return 0;
    }
 
    //char inbuff[BUFF_SIZE] = "HELLO world";
    char inbuff[BUFF_SIZE] = "한글";
    int inbufsize = sizeof(inbuff);
 
    char outbuff[BUFF_SIZE];
    int outbuffsize = sizeof(outbuff);
    memset(outbuff, 0 , outbuffsize);
 
    size_t readbyte = strlen(inbuff);
    size_t writebyte = sizeof(outbuff);
    char* in = inbuff;
    char* out = outbuff;
 
    printf("readBytes:%d writeBytes:%d\n",readbyte,writebyte);
 
    printf("%s\n",inbuff);
    if( IsUTF8Encode(inbuff) )
    {
        printf("utf-8\n");
    }
    else
    {
        printf("no utf-8\n");
    }
 
    if( iconv(cd, &in, &readbyte, &out, &writebyte) == -1)
    {
        printf("failed to iconv error\n");
    }
    else
    {
        int idx;
        printf("in:%x out:%x\n",in,out);
        printf("readBytes:%d writeBytes:%d\n",readbyte,writebyte);
        for(idx=0;idx<BUFF_SIZE;idx++)
        {
            printf("%03d %c %x\t\t", idx, inbuff[idx], inbuff[idx]);
            printf("%03d %c %x\n", idx, outbuff[idx], outbuff[idx]);
        }
 
        outbuff[writebyte] = '\0';
 
        if( IsUTF8Encode(outbuff) )
        {
            printf("utf-8\n");
        }
        else
        {
            printf("no utf-8\n");
        }
        printf("%s\n",outbuff);
    }
 
    iconv_close( cd );
    return 0;
}
세벌의 이미지

gcc -Wall -o encodetest encodetest.c
해 보니 에러 메시지가 많이 나오네요.
encodetest 는 제가 임의로 준 이름이니 편한 이름으로 쓰세요.

#include <iconv.h>
#include <stdio.h>
#include <string.h>

를 위에 붙여서 컴파일 해 보았습니다.
에러가 줄긴 했는데... 여전히 에러가 나네요.

BUFF_SIZE 가 뭔지도 모르겠고... 이건 질문한 분이 아실 듯.

하나씩 하나씩 해 보셔요.
그리고 어디에서 어떤 에러 메시지가 나는지도 설명해주시고요.

man iconv_open
잘 읽어보시고...

잘 해결하시길...

peecky의 이미지

어느 라인에서 에러가 뜨나요?

제 컴퓨터에서는 그럭저럭 다음처럼 실행됩니다.

readBytes:4 writeBytes:20
?ѱ?
in:ea387b44 out:ea387b26
readBytes:0 writeBytes:14
000 ? ffffffc7		000 ? ffffffed
001 ? ffffffd1		001 ? ffffff95
002 ? ffffffb1		002 ? ffffff9c
003 ? ffffffdb		003 ? ffffffea
004  0		004 ? ffffffb8
005  0		005 ? ffffff80
006  0		006  0
007  0		007  0
008  0		008  0
009  0		009  0
010  0		010  0
011  0		011  0
012  0		012  0
013  0		013  0
014  0		014  0
015  0		015  0
016  0		016  0
017  0		017  0
018  0		018  0
019  0		019  0
한글

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.