UTF-8 <-> UTF-16 iconv 변환 문제...
글쓴이: ohdol / 작성시간: 목, 2007/01/18 - 11:00오전
c에서 다른 인코딩으로의 iconv는 별 문제가 없는데
UTF-16으로 변환이 제대로 동작하지 않습니다.
예를 들어 UTF-8인 영문 "apple" length 5인 문자가 있으면 UTF-16로 변환시 length 10으로 변환되어야 하는데
iconv 함수가 (iconv_t)-1을 반환하고 err_str: Invalid argument, errno: 22 를 찍어주네요.
쉘에서 파일로 저장해서 하면 문제 없이 동작하는데 프로그램 코드내에서 변환하면 제대로 동작하지 않습니다.
혹시 아시는 분 답변 좀...
#include <stdio.h>
#include <iconv.h>
#include <string.h>
#include <errno.h>
#include <stdarg.h>
int ap_iconv( iconv_t iconv_a2b, char *dst, int dstmax, const char *src, int srclen )
{
int wrbytes, tmpidx;
size_t cvtsrclen, cvtdstlen, cvtlen;
char *cvtsrcptr;
char *cvtdstptr, tmpbuf[32];
FILE *fp;
int i=0;
if(iconv_a2b == (iconv_t)(-1))
return 0;
//*dst = '\0';
memset( dst, 0, dstmax );
cvtsrcptr = src;
cvtsrclen = srclen;
cvtdstptr = dst;
cvtdstlen = dstmax;
printf("srclen:%d dstmax %d\n", srclen, dstmax);
cvtlen = iconv( iconv_a2b, &cvtsrcptr, &cvtsrclen, &cvtdstptr, &cvtdstlen );
if( cvtlen == (iconv_t)-1 ) {
printf( "error:%s, %d\n", strerror(errno), errno );
return( 0 );
}
printf("iconv:%s, cvtdstlen:%u, cvtlen:%u\n", dst, cvtdstlen,cvtlen);
wrbytes = dstmax - cvtdstlen;
if( wrbytes >= 0 ) dst[wrbytes] = '\0';
printf( "cvtdstlen:%d, wrbytes:%d\n", cvtdstlen, wrbytes ); fflush( stdout );
return( wrbytes );
}
void printDebug(char *msg, ...)
{
va_list args;
va_start(args, msg);
vprintf(msg, args);
va_end(args);
printf("\n");
}
int main(int argc, char **argv)
{
char *inbuf;
char *outbuf;
size_t insize;
size_t outsize;
size_t ret;
iconv_t icon;
icon = iconv_open(argv[1], argv[2]);
//icon = iconv_open("UTF-8", "UTF-16");
inbuf = malloc(12);
sprintf(inbuf, "apple");
insize = strlen(inbuf);
outsize = insize*2;
outbuf = malloc(outsize+1);
ap_iconv(icon, outbuf, outsize, inbuf, insize);
iconv_close(icon);
free(inbuf);
free(outbuf);
return 1;
}Forums:


iconv_open 하실 때
iconv_open 하실 때 순서가 바뀐듯 싶네요.
iconv_open(to, from) 이니 iconv_open("UTF-16", "UTF-8") 식으로
하셔야 될겁니다. 나머지는 그냥 돌리면 E2BIG 에러 나던데 출력 버퍼가
작은 듯 싶습니다. 12~15 정도로 출력버퍼를 키우고 하시면 나올겁니다.
에구구
그러네요.
일정에 쫓겨서 급하게 하다보니 iconv_open쪽은 쳐다보지도 않고 다른문제인줄만 알고 삽질했네요.
거의 반나절이상 버벅이다가 외부 쉘로 실행시키는 쪽으로 코딩해 놓은 상태인데...흑~
답변 감사합니다 ^^
^^ always smile
^^ always smile
댓글 달기