update cppcommon

This commit is contained in:
gwdwyy 2013-07-12 15:34:26 +08:00
parent 05d08adb40
commit 21adc72793
2 changed files with 21 additions and 1 deletions

View File

@ -350,6 +350,24 @@ namespace CPPCOMMON
return res;
}
//utf8 -> gbk
string utf8ToGbk(const string& utf)
{
//cout<<__FILE__<<__LINE__<<gbk<<endl;
string res;
size_t maxLen = utf.size()*4;
char * pGbk = new char[maxLen];
int ret = code_convert("utf-8", "gbk", (char *)utf.c_str(), utf.size(), pGbk, maxLen);
if(ret == -1)
{
delete [] pGbk;
return "";
}
res = pGbk;
delete [] pGbk;
return res;
}
}
#ifdef TEST_STR_FUNCTS
@ -414,8 +432,9 @@ int main()
string line;
while(getline(ifile, line))
{
//cout<<line<<endl;
cout<<line<<endl;
string s = gbkToUtf8(line);
s = utf8ToGbk(s);
cout<<s<<endl;
}
return 0;

View File

@ -39,6 +39,7 @@ namespace CPPCOMMON
string utf8ToUnicode(const string& utfStr);
int code_convert(const char *from_charset,const char *to_charset,char *inbuf,size_t inlen,char *outbuf,size_t outlen);
string gbkToUtf8(const string& gbk);
string utf8ToGbk(const string& utf);
inline uint16_t twocharToUint16(char high, char low)
{