2013年1月2日水曜日

[iconv]文字コード変換

Example: iconv_test.c
#include 
#include 

const char utf8_konnichiha[] = {
 0xE3,0x81,0x93, // こ
 0xE3,0x82,0x93, // ん
 0xE3,0x81,0xAB, // に
 0xE3,0x81,0xA1, // ち
 0xE3,0x81,0xAF, // は
};

const char sjis_konnichiha[] = {
 0x82,0xB1, // こ
 0x82,0xF1, // ん
 0x82,0xC9, // に
 0x82,0xBF, // ち
 0x82,0xCD, // は
};

const char eucjp_konnichiha[] = {
 0xA4,0xB3, // こ
 0xA4,0xF3, // ん
 0xA4,0xCB, // に
 0xA4,0xC1, // ち
 0xA4,0xCF, // は
};

static int charset_convert(
    const char *from_charset,
    const char *to_charset,
    const char *src_buff,
    size_t src_size,
    char *dst_buff,
    size_t dst_size)
{
 iconv_t icd;
 char *inbuff, *outbuff;
 size_t inbuff_size, outbuff_size;
 int ret;

 /* Charset を変換する descriptor を生成する */
 icd = iconv_open(to_charset, from_charset);
 if (icd < 0) {
 fprintf(stderr, "Error: iconv_open\n");
 return -1;
 }

 inbuff = (char*)src_buff;
 inbuff_size = src_size;
 outbuff = dst_buff;
 outbuff_size = dst_size;
 ret = iconv(icd, &inbuff, &inbuff_size, &outbuff, &outbuff_size);
 printf("ret = %d inbuff_size = %d outbuff_size = %d\n",
        ret, inbuff_size, outbuff_size);
 iconv_close(icd);

 return (dst_size - outbuff_size);
}

int main(void)
{
 char buff[1024];
 int ret, i;

 /* UTF-8 から SHIFT_JIS へ変換 */
 ret = charset_convert(
                       "UTF-8",
                       "SHIFT_JIS",
                       utf8_konnichiha,
                       sizeof(utf8_konnichiha),
                       buff,
                       sizeof(buff));
 if (ret > 0) {
   /* 結果表示 */
   printf("Input (UTF-8) : ");
   for (i = 0; i < sizeof(utf8_konnichiha); i++) {
     printf("%02x ", (utf8_konnichiha[i] & 0x000000ff));
   }
   printf("\n");
   printf("Output(SHIFT_JIS): ");
   for (i = 0; i < ret; i++) {
     printf("%02x ", (buff[i] & 0x000000ff));
   }
   printf("\n");
 }

 /* UTF-8 から EUC-JP へ変換 */
 ret = charset_convert(
                       "UTF-8",
                       "EUC-JP",
                       utf8_konnichiha,
                       sizeof(utf8_konnichiha),
                       buff,
                       sizeof(buff));
 if (ret > 0) {
   /* 結果表示 */
   printf("Input (UTF-8) : ");
   for (i = 0; i < sizeof(utf8_konnichiha); i++) {
     printf("%02x ", (utf8_konnichiha[i] & 0x000000ff));
   }
   printf("\n");
   printf("Output(EUC-JP) : ");
   for (i = 0; i < ret; i++) {
     printf("%02x ", (buff[i] & 0x000000ff));
   }
   printf("\n");
 }

 return 0;
}
コンパイル
> gcc -Wall -o iconv_test.exe iconv_test.c -liconv -static
実行結果
> iconv_test.exe
ret = 0 inbuff_size = 0 outbuff_size = 1014
Input (UTF-8) : e3 81 93 e3 82 93 e3 81 ab e3 81 a1 e3 81 af
Output(SHIFT_JIS): 82 b1 82 f1 82 c9 82 bf 82 cd
ret = 0 inbuff_size = 0 outbuff_size = 1014
Input (UTF-8) : e3 81 93 e3 82 93 e3 81 ab e3 81 a1 e3 81 af
Output(EUC-JP) : a4 b3 a4 f3 a4 cb a4 c1 a4 cf

0 件のコメント:

コメントを投稿