双11狂欢大促

盲盒赢 定制羽绒服

开启惊喜盲盒,抽取你的专属好礼

— 惊喜奖品抢先看 —

Linux下的iconv函数

写了程序换酒钱 写了程序换酒钱 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
在做utf-8编码转成unicode,使用了iconv函数
下面是代码

#ifndef CODECONVERTER_H
#define CODECONVERTER_H

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

class CodeConverter
{
public:
CodeConverter(const char *from,const char *to) {
id = iconv_open(to,from);
}
~CodeConverter() {
iconv_close(id);
}

int converter(char *input,size_t &inlen,char *output,size_t &outlen) {
char **in = &input;
char **out = &output;
return iconv(id,in,&inlen,out,&outlen);
}



private:
iconv_t id;

};

#endif // CODECONVERTER_H


#include <iostream>
#include "CodeConverter.h"
#include <cstring>

using namespace std;

int main()
{
char *msg = "你好";

char out[20] = {'\0'};
size_t outl = sizeof(out);
size_t inl = strlen(msg);
CodeConverter converter("utf-8","unicode");
int r = converter.converter(msg,inl,out,outl);
if ( r != -1) {
// cout <<"len:"<< inl <<" "<<outl <<"unicode:"<< out << endl;
for (int j = 0;j < 20 - (int)outl;++j) {
cout << j << " : " << hex <<((unsigned short int)out[j] & 0x00ff)<< '\n';
}

}


是正确的,不过就是位置需要调下。


可是为什么每次运行的时候,开始两个都是ff和fe了?求大虾解释下,求教
...全文
评论 2
收藏 已收藏
分享
返回顶部
写了程序换酒钱
C++ 语言 65,214 成员 250,514 内容