日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

多字节与UTF-8、Unicode之间的转换

發(fā)布時間:2024/4/14 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 多字节与UTF-8、Unicode之间的转换 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

from?http://blog.csdn.net/frankiewang008/article/details/12832239

?

  • //?多字節(jié)編碼轉(zhuǎn)為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編碼轉(zhuǎn)為多字節(jié)編碼??
  • 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;??
  • }??
  • ??
  • //?多字節(jié)編碼轉(zhuǎn)為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編碼轉(zhuǎn)為多字節(jié)編碼??
  • 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編碼轉(zhuǎn)為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編碼轉(zhuǎn)為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;??
  • } ?
  • 總結(jié)

    以上是生活随笔為你收集整理的多字节与UTF-8、Unicode之间的转换的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。