Linux下的iconv函数

写了程序换酒钱 2013-03-04 09:57:25
在做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了?求大虾解释下,求教
...全文
187 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2013-03-05
  • 打赏
  • 举报
回复
搜“BOM”
图灵狗 2013-03-05
  • 打赏
  • 举报
回复
unicode就是以0xff/0xfe或者是0xfe/0xff开始,它们的顺序决定字节序的大小端区别。
  • 打赏
  • 举报
回复
谢拉,我这是在x86上写的,x86是大端,烧到板子arm的是小段,还要修改

65,186

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧