[C/C++]_[使用libiconv库转换字符编码]

论坛 期权论坛 脚本     
匿名技术用户   2021-1-4 12:39   104   0


场景:

1.在windows上我们可以通过WideCharToMultiByte和MultiByteToWideChar直接转换或间接转换编码,但是在linux或mac上却没有那么方便的系统api了,这时候可以使用libiconv库来进行转码,质量还是很高的。

2.以下我们把utf8编码字符串转换为utf16-le(小端序)编码。


文件test_iconv.cpp,是utf8编码。

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

int GetUtf8LetterNumber(const char *s)
{
  int i = 0, j = 0;
  while (s[i])
  {
    if ((s[i] & 0xc0) != 0x80) j++;
    i++;
  }
  return j;
}


int main(int argc,char *argv[])
{
 
 iconv_t cd = NULL;
 cd = iconv_open("UTF-16LE", "UTF-8");

 char *inptr = (char*)"我们的国家";
 char* inptr1 = inptr;
 size_t length_i = strlen(inptr);

 printf("length_i %d\n",length_i);
 size_t length = GetUtf8LetterNumber(inptr)*2;
 size_t lengtht = length;
 printf("length %d\n",length);

 char* temp_dest = (char*)malloc(length+2);
 char* temp_destt = temp_dest;
 memset(temp_dest,0,length+2);

 size_t result = iconv(cd, &inptr1, &length_i, &temp_dest, &length);

 if (result != (size_t) -1)
 {
  iconv_close(cd);
 }

 printf("length %d\n",length);
 printf("length_i %d\n",length_i);
 for(int i = 0;i<lengtht;++i)
 {
  printf("%x\n",(unsigned char)temp_destt[i]);
 }
}

输出utf16-le的16进制编码.

length_i 15
length 10
length 0
length_i 0
11
62
ec
4e
84
76
fd
56
b6
5b

注意:参数length_i和length是递减的,这样原来的值会丢失,所以重新赋给新的值。

size_t iconv (iconv_t cd,
              const char* * inbuf, size_t * inbytesleft,
              char* * outbuf, size_t * outbytesleft);
The iconv function converts one multibyte character at a time, and for each character conversion it increments *inbuf and
decrements *inbytesleft by the number of converted input bytes, it increments *outbuf and decrements *outbytesleft by the number 
of converted output bytes, and it updates the conversion state contained in cd.
看这里有iconv函数解释地址:

http://www.gnu.org/savannah-checkouts/gnu/libiconv/documentation/libiconv-1.13/iconv.3.html

这里是支持的编解码列表:

http://www.gnu.org/savannah-checkouts/gnu/libiconv/documentation/libiconv-1.13/iconv_open.3.html


分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:7942463
帖子:1588486
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP