多位元組編碼字串與UTF8字元編碼的轉換

2021-06-25 13:33:49 字數 1484 閱讀 6779

1、多位元組編碼字串轉utf8字元編碼

std::string csqlitedbmanager::to_utf8(std::string strdata)

//轉換乙個寬字串到utf8字串

int iutf8len = ::widechartomultibyte(cp_utf8, 0, lpszw, ilen, null, 0, null, null);

if(0 == iutf8len)

char* putf8 = new char[iutf8len];

::memset(putf8, 0, iutf8len);

::widechartomultibyte(cp_utf8, 0, lpszw, ilen, putf8, iutf8len, null, null);

delete pmb;pmb= null;delete lpszw;lpszw = null;

delete putf8;putf8= null;

std::string strtemp = putf8;

return strtemp;

}

2、utf8字元編碼轉多位元組字元編碼

/*將utf8字元轉換成多位元組編碼字串*/

//將utf8字串轉成寬字元字串

int utf8len = strlen(ptempelem->gettext());//ptempelem->gettext()返回的是utf8字元編碼字串

int ilen = multibytetowidechar(cp_utf8, 0, ptempelem->gettext(), utf8len, null, 0);

wchar* lpszw = null;

lpszw = new wchar[ilen];

wmemset(lpszw, 0, ilen);

int irtn = multibytetowidechar(cp_utf8, 0, ptempelem->gettext(), utf8len, lpszw, ilen);

if(irtn != ilen)

//轉換寬字元字串到多位元組字串

int imblen = widechartomultibyte(cp_acp, 0, lpszw, ilen, null, 0, null, null);

if(0 == imblen)

char* pmbstr = new char[imblen];

::memset(pmbstr, 0, imblen);

irtn = widechartomultibyte(cp_acp, 0, lpszw, ilen, pmbstr, imblen, null, null);

delete lpszw;

if(irtn != imblen)

string strdatatem = pmbstr;

UTF 8與多位元組字串轉化

utf 8轉本地編碼以及本地編碼轉utf 8 寬字元字串轉多位元組字串 inline std string w2a const wchar t pwsztext int isizeinbytes widechartomultibyte cp acp,0,pwsztext,1 null,0 null ...

擷取處理UTF 8編碼字串

擷取處理utf 8編碼字串 規則 中文2個字元,數字 英文1個字元 擷取末尾不足乙個漢字的則捨棄。param string str param int len 截取出的字元長度 author flyer0126 since 2012 05 03 function substr utf8 cn str...

多位元組多編碼字串擷取

字串擷取,支援中文和其他編碼 static access public param string str 需要轉換的字串 param string start 開始位置 param string length 擷取長度 param string charset 編碼格式 param string s...