mirror of
https://github.com/yanyiwu/cppjieba.git
synced 2025-07-18 00:00:12 +08:00
update cppcommon
This commit is contained in:
parent
05d08adb40
commit
21adc72793
@ -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;
|
||||
|
@ -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)
|
||||
{
|
||||
|
Loading…
x
Reference in New Issue
Block a user