Contents

Develop
2013.04.23 15:49

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

조회 수 20116 댓글 0
Atachment
첨부 '1'
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄

첨부된 파일은 아래 내용과 같이 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);
}


?

List of Articles
번호 분류 제목 글쓴이 날짜 조회 수
733 Develop [swift] UIView에서 subview 찾기 hooni 2022.12.09 1272
732 Develop [ios] GPS 이용 상태 확인 hooni 2015.04.27 1293
731 Develop [js] URL 파싱하기 (jQuery 안쓰고) hooni 2017.12.14 1297
730 Develop [android] 간단한 SQLIite 예제 hooni 2017.06.14 1306
729 Develop [php] 한글 문자열 자르기 (utf-8) hooni 2015.11.10 1309
728 Develop How to Test SMTP AUTH using Telnet hooni 2018.04.05 1323
727 Develop [ios] How to set up clang formatter hooni 2015.09.17 1349
726 Develop [iOS] Xcode 불필요한 캐시 삭제하기 hooni 2021.10.12 1351
725 Develop [swift] popToRoot 모달뷰, 네비게이션컨트롤러 한꺼번에 닫기 file hooni 2021.01.29 1370
724 Develop [ios] Swift 4 String, Date, DateFormatter 예제 hooni 2018.10.18 1385
723 Develop What is difference between Get, Post, Put and Delete? hooni 2018.02.28 1391
722 Develop [android] 레이아웃 사이즈 변경 (동적; programmatically) hooni 2016.11.07 1472
Board Pagination Prev 1 ... 5 6 7 8 9 10 11 12 13 14 ... 71 Next
/ 71