思路:
获取字符串里面中的Unicode部分,然后将该部分转换位utf-8格式的字符,最后将字符串里面的所有Unicode替换为utf-8即可。
废话不多少,直接上代码:
头文件:
/*
* charsetEncode.h
*
* Created on: Jul 25, 2016
* Author: root
*/
#ifndef COMMONSERVER_INCLUDE_CHARSETENCODE_H_
#define COMMONSERVER_INCLUDE_CHARSETENCODE_H_
#include <iostream>
#include <algorithm>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <assert.h>
using namespace std;
class CcharsetEncode
{
public:
int unicode_to_utf8(string &source);//unicode to utf-8
//字符串忽略大小写字串替换
void ReplaceStr(string &strContent, const char *strSrc, const char *strDest);
private:
int enc_unicode_to_utf8_one(unsigned long unic, unsigned char *pOutput, int outSize);
int isUnicode(const string &src); //the src is unicode or not, total 6 char(0x5e3f).1,yes;2,no
unsigned int xstrtoshortint(const char *str); //"0x1a3f"->1a3f
};
#endif /* COMMONSERVER_INCLUDE_CHARSETENCODE_H_ */
源文件:
/*
* char