C++ GB2312 和 utf8 在win32下 互轉

來源:互聯網
上載者:User

標籤:[]   col   span   echart   art   return   turn   簡體   gpo   

 1 string ANSItoUTF8(const char* strAnsi) 2 { 3     //擷取轉換為寬位元組後需要的緩衝區大小,建立寬位元組緩衝區,936為簡體中文GB2312字碼頁   4     int nLen = MultiByteToWideChar(CP_ACP, NULL, strAnsi, -1, NULL, NULL); 5     WCHAR *wszBuffer = new WCHAR[nLen + 1]; 6     nLen = MultiByteToWideChar(CP_ACP, NULL, strAnsi, -1, wszBuffer, nLen); 7     wszBuffer[nLen] = 0; 8     //擷取轉為UTF8多位元組後需要的緩衝區大小,建立多位元組緩衝區   9     nLen = WideCharToMultiByte(CP_UTF8, NULL, wszBuffer, -1, NULL, NULL, NULL, NULL);10     CHAR *szBuffer = new CHAR[nLen + 1];11     nLen = WideCharToMultiByte(CP_UTF8, NULL, wszBuffer, -1, szBuffer, nLen, NULL, NULL);12     szBuffer[nLen] = 0;13 14     string s1 = szBuffer;15     //記憶體清理  16     delete[]wszBuffer;17     delete[]szBuffer;18     return s1;19 }20 21 string UTF8toANSI(const char* strUTF8)22 {23     //擷取轉換為多位元組後需要的緩衝區大小,建立多位元組緩衝區  24     int nLen = MultiByteToWideChar(CP_UTF8, NULL, strUTF8, -1, NULL, NULL);25     WCHAR *wszBuffer = new WCHAR[nLen + 1];26     nLen = MultiByteToWideChar(CP_UTF8, NULL, strUTF8, -1, wszBuffer, nLen);27     wszBuffer[nLen] = 0;28 29     nLen = WideCharToMultiByte(936, NULL, wszBuffer, -1, NULL, NULL, NULL, NULL);30     CHAR *szBuffer = new CHAR[nLen + 1];31     nLen = WideCharToMultiByte(936, NULL, wszBuffer, -1, szBuffer, nLen, NULL, NULL);32     szBuffer[nLen] = 0;33 34     string s1 = szBuffer;35     //清理記憶體  36     delete[]szBuffer;37     delete[]wszBuffer;38     return s1;39 }

轉載

C++ GB2312 和 utf8 在win32下 互轉

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.