Contents

Develop
2013.04.23 15:49

[c] UTF-8을 EUC-KR로 변환.. (iconv)

Views 20129 Comment 0
Atachment
Attachment '1'
?

Shortcut

PrevPrev Article

NextNext Article

Larger Font Smaller Font Up Down Go comment Print
?

Shortcut

PrevPrev Article

NextNext Article

Larger Font Smaller Font Up Down Go comment Print

첨부된 파일은 아래 내용과 같이 make 버전으로 심플하게 수정한것도 포함됨..

iconv 라이브러리 좋은 사용 예제..

/*
iconv를 활용한 코드 변환 (EUC-KR <-> UTF-8)
gcc 버전에 따라 glibc에 포함된 경우는 -lc를 하고 그렇지 않은 경우는 -liconv를 링크한다.
*/

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


int main()
{
    int ret;
    iconv_t it;
    char ksc_buf[1024] = "한글과 유니코드";
    
    // KSC(완성형) 코드를 UTF(유니코드)로 변환하면 원래 크기보다 커지므로 크게~
    char utf_buf[1024] = {0x00, };
    size_t in_size, out_size;

    sprintf(ksc_buf, "%s", "한글과 유니코드");
    memset(utf_buf, '\0', 1024);

    // 어떤 시스템에서는 char** 가 아니라 const char** 인 경우도 있음
    char *input_buf_ptr = ksc_buf;
    char *output_buf_ptr = utf_buf;

    in_size = strlen(ksc_buf);
    out_size = sizeof(utf_buf);

    it = iconv_open("UTF-8", "EUC-KR"); // EUC-KR을 UTF-8로
    ret = iconv(it, &input_buf_ptr, &in_size, &output_buf_ptr, &out_size);
    
    if (ret < 0)
    {
        printf("ret : %d, errno : %d\n", ret, errno);
        return(-1);
    }
    else
    {
        printf("[%s](%d) => [%s][(%d)\n",
            ksc_buf, in_size, utf_buf, out_size);
    }
    
    iconv_close(it);

    input_buf_ptr = utf_buf;
    output_buf_ptr = ksc_buf;

    in_size = strlen(utf_buf);
    out_size = sizeof(ksc_buf);

    it = iconv_open("EUC-KR", "UTF-8"); // UTF-8을 EUC-KR로
    ret = iconv(it, &input_buf_ptr, &in_size, &output_buf_ptr, &out_size);

    if (ret < 0)
    {
        printf("ret : %d, errno : %d\n", ret, errno);
        return(-1);
    }
    else
    {
        printf("[%s](%d) => [%s][(%d)\n",
            utf_buf, in_size, ksc_buf, out_size);
    }
    
    iconv_close(it);
}


?

  1. [c] 가변인자 함수(printf와 같은..)

    Date2013.04.23 CategoryDevelop Byhooni Views7173
    Read More
  2. [linux] 한글 URL 인식할 수 있게 아파치(Apache) 설정 (mod_url.c 설치)

    Date2013.04.23 CategorySystem/OS Byhooni Views13060
    Read More
  3. [c] openssl 샘플코드.. 어려움 ㅠㅠ

    Date2013.04.23 CategoryDevelop Byhooni Views7020
    Read More
  4. [c] 바로 보고 정리할 것.. ㅋㄷㅋㄷ

    Date2013.04.23 CategoryDevelop Byhooni Views7131
    Read More
  5. [c] UTF-8을 EUC-KR로 변환.. (iconv)

    Date2013.04.23 CategoryDevelop Byhooni Views20129
    Read More
  6. [c] 도메인(호스트)으로 IP정보 알아오기.. (nslookup과 비슷)

    Date2013.04.23 CategoryDevelop Byhooni Views6927
    Read More
  7. [c] 파이프(popen)로 다른 프로세스 실행결과 가져오기

    Date2013.04.23 CategoryDevelop Byhooni Views8394
    Read More
  8. [c++] Win32 API 기본 출력인 MessageBox() 함수 사용 예제..

    Date2013.04.23 CategoryDevelop Byhooni Views12631
    Read More
  9. [c++] mfc에서 윈도우 항상 위 속성 주기..

    Date2013.04.23 CategoryDevelop Byhooni Views11299
    Read More
  10. [c] 쓰레드에 대한 내용 퍼오기..ㅡㅡ;

    Date2013.04.23 CategoryDevelop Byhooni Views8430
    Read More
  11. [c++] 채팅로봇 소스.. ㅋㄷㅋㄷ

    Date2013.04.23 CategoryDevelop Byhooni Views8878
    Read More
  12. [doc] 인공지능 관련 자료(채팅로봇도 포함..)

    Date2013.04.23 CategoryPPT Byhooni Views18001
    Read More
Board Pagination Prev 1 ... 41 42 43 44 45 46 47 48 49 50 ... 98 Next
/ 98