linux 下 string 中文乱码问题解决

以下是实现

#include <iconv.h>

extern std::vector<std::string> vecImageId;
int code_convert(char *from_charset, char *to_charset, char *inbuf, size_t inlen, char *outbuf, size_t outlen) {
iconv_t cd;
char **pin = &inbuf;
char **pout = &outbuf;


cd = iconv_open(to_charset, from_charset);
if (cd == 0)
return -1;
memset(outbuf, 0, outlen);
if (iconv(cd, pin, &inlen, pout, &outlen) == -1)
return -1;
iconv_close(cd);
*pout = '\0';


return 0;
}

//将UTF8转为GBK
int u2g(char *inbuf, size_t inlen, char *outbuf, size_t outlen) {
return code_convert("utf-8", "gb2312", inbuf, inlen, outbuf, outlen);
}

//将GBK转为UTF8
int g2u(char *inbuf, size_t inlen, char *outbuf, size_t outlen) {
return code_convert("gb2312", "utf-8", inbuf, inlen, outbuf, outlen);

}


以下是使用:

std::string ObjExtVlprGLST::getVehStyle(std::string style)

{
char* str;
char outstr[128] = { 0 };
std::string str1 = "";
if (style == "1")
{
str = "轿车";
}
else if (style == "2")
{
str = "越野车";
}
g2u(str, strlen(str), outstr, 128);
str1 = outstr;
return str1;

}

这样可以解决中文乱码问题。

阅读更多
换一批

没有更多推荐了,返回首页