iconv() 질문

kwon37xi의 이미지

EUC-KR과 Unicode간 전환이 필요해서 iconv()를 이용하려고 합니다만,
맨페이지를 봐도 영어가 딸려 이해가 잘 안가는군요.
다음과 같은 코드를 만들었는데 전혀 작동하지 않습니다. 다음은 단지 iconv() 사용법을 익히기위한 테스트용입니다.

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

int main()
{
	char * str = "EUC-KR English.";

	char * out = malloc(sizeof(char)*1000);
	memset((void *)out,0, (size_t)1000);
	
	printf("%s:%d 글자출력\n",str, strlen(str));

	iconv_t cd = iconv_open("utf-8", "euc-kr");
	if ((int)cd <= 0) {
		printf("에러\n");
		exit(0);
	}

	size_t inbytesleft = strlen(str);
	size_t outbytesleft = 999;
	
	size_t st = iconv(cd, &str, &inbytesleft, &out, &outbytesleft);
	printf("st-%d,size of out-%d,%s:inleft-%d,outleft-%d\n",st,strlen(out),out,inbytesleft, outbytesleft);
	iconv_close(cd);
	return 0;
}

출력 결과는

Quote:
EUC-KR English.:15 글자출력
st-0,size of out-0,:inleft-0,outleft-984

일단 제가 생각하는 iconv()의 작동을 말씀드리면
inbytesleft 는 소스문자열의 byte크기를 의미하고, outbytesleft는 출력 문자열 버퍼의 크기를 의미합니다. iconv()가 직접 한바이트 한바이트 변환을 하면서 inbytesleft의 값을 바이트당 1씩 감소시킵니다. 그리고 outbytesleft는 인코딩된 문자열이 차지하는 bytes 수만큼의 값을 뺀 나머지 값을 가지게 됩니다.
out 에는 변환된 문자열이 위치하게 되구요.

제가 iconv()의 어떤 부분을 잘못알고 있는 걸까요??
알파벳 코드의 utf-8과 euc-kr의 값이 동일하다는 것은 알고 있습니다.

mykldp의 이미지

Quote:
out 에는 변환된 문자열이 위치하게 되구요.

그렇지 않습니다. iconv 가 변환을 하면서 out 과 str 의 값을 증가시킵니다.


...

char * out = malloc(...)
char * out_p = out;

...

iconv(cd, &str, &inbytesleft, &out_p, &outbytesleft);

...

이렇게 하시면 이 때 out 이 변환된 문자열을 가리키는 포인터가 됩니다.

out_p 는 (변환된 문자열의 마지막 바이트 + 1) 의 메모리를 가리키는 포인터가 되구요.

여기 게시판에서 iconv로 검색을 하시면 참고하실 내용이 많이 나올겁니다.

kwon37xi의 이미지

감사합니다.
상당히 단순한 것을 이해하지 못하고 있었군요. 덕분에 문제 해결됐습니다.
iRiver IFP Driver(http://ifp-driver.sf.net)가 ASCII 문자만 지원해서 EUC-KR 한글 패치를 만들었어요.
첨으로 패치란걸 해봤군요.

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.