iconv示例

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <iostream>
#include <string>
#include <iconv.h>
#include <errno.h>

int main(){
    // iconv_t pt=iconv_open("GB2312","UTF-8");
    iconv_t pt=iconv_open("UTF-16","GB2312");
    if(pt== (iconv_t)-1){
    std::cout<<"iconv_open 失败"<<std::endl;
    }
    iconv(pt,NULL,NULL,NULL,NULL);
    const char * str="好";
    char buf[100];
    char* out=new char[100];
    size_t b=sizeof(out);
    const char** in=&str;
    size_t lenin=strlen(str);
    int ret=iconv(pt,in,&lenin,&out,&b);
    if(ret){
    std::cout<<"转换失败\n";
    switch(errno){
    case E2BIG:
    std::cout<<"There is not sufficient room at *outbuf.\n";
    break;
    case EILSEQ:
    std::cout<<" An invalid multibyte sequence has "
    "been encountered in the input.\n";
    break;
    case EINVAL:
    std::cout<<" An incomplete multibyte sequence has been
    encountered in the input.\n";
    break;
    default:
    std::cout<<"未知的errno\n";
    }
    }
    printf("ret=%d, len_in=%d, len_out=%d\n",
    ret, lenin, b);
    iconv_close(pt);
    return 0;
}

此博客中的热门博文

少写代码,多读别人写的代码

在windows下使用llvm+clang

tensorflow distributed runtime初窥