多位元組與UTF-8、Unicode之間的轉換

來源:互聯網
上載者:User

// 多位元組編碼轉為UTF8編碼 bool MBToUTF8(vector<char>& pu8, const char* pmb, int32 mLen) { // convert an MBCS string to widechar int32 nLen = MultiByteToWideChar(CP_ACP, 0, pmb, mLen, NULL, 0); WCHAR* lpszW = NULL; try { lpszW = new WCHAR[nLen]; } catch(bad_alloc &memExp) { return false; } int32 nRtn = MultiByteToWideChar(CP_ACP, 0, pmb, mLen, lpszW, nLen); if(nRtn != nLen) { delete[] lpszW; return false; } // convert an widechar string to utf8 int32 utf8Len = WideCharToMultiByte(CP_UTF8, 0, lpszW, nLen, NULL, 0, NULL, NULL); if (utf8Len <= 0) { return false; } pu8.resize(utf8Len); nRtn = WideCharToMultiByte(CP_UTF8, 0, lpszW, nLen, &*pu8.begin(), utf8Len, NULL, NULL); delete[] lpszW; if (nRtn != utf8Len) { pu8.clear(); return false; } return true; } // UTF8編碼轉為多位元組編碼 bool UTF8ToMB(vector<char>& pmb, const char* pu8, int32 utf8Len) { // convert an UTF8 string to widechar int32 nLen = MultiByteToWideChar(CP_UTF8, 0, pu8, utf8Len, NULL, 0); WCHAR* lpszW = NULL; try { lpszW = new WCHAR[nLen]; } catch(bad_alloc &memExp) { return false; } int32 nRtn = MultiByteToWideChar(CP_UTF8, 0, pu8, utf8Len, lpszW, nLen); if(nRtn != nLen) { delete[] lpszW; return false; } // convert an widechar string to Multibyte int32 MBLen = WideCharToMultiByte(CP_ACP, 0, lpszW, nLen, NULL, 0, NULL, NULL); if (MBLen <=0) { return false; } pmb.resize(MBLen); nRtn = WideCharToMultiByte(CP_ACP, 0, lpszW, nLen, &*pmb.begin(), MBLen, NULL, NULL); delete[] lpszW; if(nRtn != MBLen) { pmb.clear(); return false; } return true; } // 多位元組編碼轉為Unicode編碼 bool MBToUnicode(vector<wchar_t>& pun, const char* pmb, int32 mLen) { // convert an MBCS string to widechar int32 uLen = MultiByteToWideChar(CP_ACP, 0, pmb, mLen, NULL, 0); if (uLen<=0) { return false; } pun.resize(uLen); int32 nRtn = MultiByteToWideChar(CP_ACP, 0, pmb, mLen, &*pun.begin(), uLen); if (nRtn != uLen) { pun.clear(); return false; } return true; } //Unicode編碼轉為多位元組編碼 bool UnicodeToMB(vector<char>& pmb, const wchar_t* pun, int32 uLen) { // convert an widechar string to Multibyte int32 MBLen = WideCharToMultiByte(CP_ACP, 0, pun, uLen, NULL, 0, NULL, NULL); if (MBLen <=0) { return false; } pmb.resize(MBLen); int nRtn = WideCharToMultiByte(CP_ACP, 0, pun, uLen, &*pmb.begin(), MBLen, NULL, NULL); if(nRtn != MBLen) { pmb.clear(); return false; } return true; } // UTF8編碼轉為Unicode bool UTF8ToUnicode(vector<wchar_t>& pun, const char* pu8, int32 utf8Len) { // convert an UTF8 string to widechar int32 nLen = MultiByteToWideChar(CP_UTF8, 0, pu8, utf8Len, NULL, 0); if (nLen <=0) { return false; } pun.resize(nLen); int32 nRtn = MultiByteToWideChar(CP_UTF8, 0, pu8, utf8Len, &*pun.begin(), nLen); if(nRtn != nLen) { pun.clear(); return false; } return true; } // Unicode編碼轉為UTF8 bool UnicodeToUTF8(vector<char>& pu8, const wchar_t* pun, int32 uLen) { // convert an widechar string to utf8 int32 utf8Len = WideCharToMultiByte(CP_UTF8, 0, pun, uLen, NULL, 0, NULL, NULL); if (utf8Len<=0) { return false; } pu8.resize(utf8Len); int32 nRtn = WideCharToMultiByte(CP_UTF8, 0, pun, uLen, &*pu8.begin(), utf8Len, NULL, NULL); if (nRtn != utf8Len) { pu8.clear(); return false; } return true; }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.