浠婂ぉ鍦╳indows鎺у埗鍙颁笂鎵撳嵃utf-8瀛楃鏃跺嚭鐜颁簡涔辩爜锛岀劧鍚庡氨鎶樿吘浜嗕竴涓嬪彂鐜板湪绠�浣撲腑鏂囩増涓婄殑windows榛樿鐨勪唬鐮侀〉鏄�936锛坓bk缂栫爜锛夛紝鍦ㄦ帶鍒跺彴涓婅緭鍏�chcp 65001瑙e喅涔嬶紙65001鏄疷TF-8浠g爜椤电紪鍙凤級锛屼絾鏄垜杩欎箞鐖遍捇鐮旓紙鐬庢姌鑵撅級鎬庝箞鍙兘灏辫繖涔堝畬浜嬩簡鍛紝灏卞皾璇曚簡涓嬬敤C璇█瀹炵幇utf-8杞寲鎴恎bk缂栫爜锛堣繕椤轰究瀛︿範浜嗕笅鍑犵缂栫爜锛夛紝涓嬮潰鏄В鍐虫柟娉曘��
鍡�傘�傝繕鏈夋垜瀛︿範鏃剁湅鐨勪竴涓祫鏂欙紝浠嬬粛缂栫爜鐭ヨ瘑鐨�
http://www.ruanyifeng.com/blog/2007/10/ascii_unicode_and_utf-8.html
瑙e喅鏂规硶
1.浣跨敤浠g爜鏇存敼娲诲姩浠g爜椤�
system("chcp 65001");
涔熷彲浠ヤ娇鐢╳indows鐨勪竴涓狝PI锛堣缃緭鍑轰唬鐮侀〉锛夛紝鏁堟灉涓�鏍�
SetConsoleOutputCP(65001);
杩樻湁鍙﹀涓�涓狝PI鏄疭etConsoleCP()锛岃繖涓狝PI璁剧疆鐨勬槸杈撳叆浠g爜椤碉紝鍦ㄨ緭鍑烘椂骞朵笉璧蜂綔鐢�
C璇█鏍囧噯閲岄潰杩樻彁渚涗簡涓�涓嚱鏁版潵璁剧疆浠g爜椤礢etlocale()锛屼笉杩囨病鏈夋祴璇曡繃
2.灏唘tf-8杞崲鎴恎bk缂栫爜
鍘熺悊鏄埄鐢╳indows鐨勪袱涓狝PI锛屽皢UTF-8杞垚unicode缂栫爜锛屽啀杞垚gbk缂栫爜
涓嬮潰鏄涓や釜鍑芥暟鐨勪粙缁�
鍑芥暟鍘熷瀷
int MultiByteToWideChar(
UINT CodePage,
DWORD dwFlags,
LPCSTR lpMultiByteStr,
int cchMultiByte,
LPWSTR lpWideCharStr,
int cchWideChar);
鍑芥暟鍔熻兘聽
璇ュ嚱鏁版槧灏勪竴涓瓧绗︿覆鍒颁竴涓瀛楃锛坲nicode锛夌殑瀛楃涓层�傜敱璇ュ嚱鏁版槧灏勭殑瀛楃涓叉病蹇呰鏄瀛楄妭瀛楃缁勩��
鍙傛暟
CodePage锛氭寚瀹氭墽琛岃浆鎹㈢殑浠g爜椤碉紝杩欎釜鍙傛暟鍙互涓虹郴缁熷凡瀹夎鎴栨湁鏁堢殑浠讳綍浠g爜椤垫墍缁欏畾鐨勫�笺�傛垜浠敤鍒扮殑鏄笅闈袱涓�硷紙褰撶劧浣犱篃鍙互鎸囧畾鍒殑鍊硷級锛�
CP_ACP锛欰NSI浠g爜椤碉紙绠�浣撲腑鏂嘩indows鎿嶄綔绯荤粺涓紝ANSI 缂栫爜浠h〃 GBK 缂栫爜锛�
CP_UTF8锛氫娇鐢║TF-8杞崲
dwFlags锛� 鎸囧畾濡備綍澶勭悊娌℃湁杞崲鐨勫瓧绗︼紝浣嗕笉璁炬鍙傛暟鍑芥暟浼氳繍琛岀殑鏇村揩涓�浜涳紝鎴戦兘鏄妸瀹冭涓�0銆� 鍙鐨勫�煎涓嬭〃鎵�绀�:
WC_NO_BEST_FIT_CHARS
鎶婁笉鑳界洿鎺ヨ浆鎹㈡垚鐩稿簲澶氬瓧鑺傚瓧绗︾殑Unicode瀛楃杞崲鎴恖pDefaultChar鎸囧畾鐨勯粯璁ゅ瓧绗︺�備篃灏辨槸璇达紝濡傛灉鎶奤nicode杞崲鎴愬瀛楄妭瀛楃锛岀劧鍚庡啀杞崲鍥炴潵锛屼綘骞朵笉涓�瀹氬緱鍒扮浉鍚岀殑Unicode瀛楃锛屽洜涓鸿繖鏈熼棿鍙兘浣跨敤浜嗛粯璁ゅ瓧绗︺�傛閫夐」鍙互鍗曠嫭浣跨敤锛屼篃鍙互鍜屽叾浠栭�夐」涓�璧蜂娇鐢ㄣ��
WC_COMPOSITECHECK
鎶婂悎鎴愬瓧绗﹁浆鎹㈡垚棰勫埗鐨勫瓧绗︺�傚畠鍙互涓庡悗涓変釜閫夐」涓殑浠讳綍涓�涓粍鍚堜娇鐢紝濡傛灉娌℃湁涓庝粬浠腑鐨勪换浣曚竴涓粍鍚堬紝鍒欎笌閫夐」WC_SEPCHARS鐩稿悓銆�
WC_ERR_INVALID_CHARS
姝ら�夐」浼氳嚧浣垮嚱鏁伴亣鍒版棤鏁堝瓧绗︽椂澶辫触杩斿洖锛屽苟涓擥etLastError浼氳繑鍥為敊璇爜ERROR_NO_UNICODE_TRANSLATION銆傚惁鍒欏嚱鏁颁細鑷姩涓㈠純闈炴硶瀛楃銆傛閫夐」鍙兘鐢ㄤ簬UTF8銆�
WC_DISCARDNS聽 聽
杞崲鏃朵涪寮冧笉鍗犵┖闂寸殑瀛楃锛屼笌WC_COMPOSITECHECK 涓�璧蜂娇鐢�
WC_SEPCHARS
杞崲鏃朵骇鐢熷崟鐙殑瀛楃锛屾鏄粯璁よ浆鎹㈤�夐」锛學C_COMPOSITECHECK涓�璧蜂娇鐢�
WC_DEFAULTCHAR
杞崲鏃朵娇鐢ㄩ粯璁ゅ瓧绗︿唬鏇夸緥澶栫殑瀛楃锛�(鏈�甯歌鐨勫鈥�?鈥�)锛屼笌WC_COMPOSITECHECK涓�璧蜂娇鐢ㄣ��
瀵逛簬涓嬪垪浠g爜椤碉紝dwFlags蹇呴』涓�0锛屽惁鍒欏嚱鏁拌繑鍥為敊璇爜ERROR_INVALID_FLAGS銆� 50220 50221 50222 50225 50227 50229 52936 54936 57002鍒�57011 65000(UTF7) 42(Symbol)
瀵逛簬UTF8锛宒wFlags蹇呴』涓�0鎴朩C_ERR_INVALID_CHARS锛屽惁鍒欏嚱鏁伴兘灏嗗け璐ヨ繑鍥炲苟璁剧疆閿欒鐮丒RROR_INVALID_FLAGS锛屼綘鍙互璋冪敤GetLastError鑾峰緱銆�
lpMultiByteStr锛氭寚鍚戝皢琚浆鎹㈠瓧绗︿覆鐨勫瓧绗︺��
cchMultiByte锛氭寚瀹氱敱鍙傛暟lpMultiByteStr鎸囧悜鐨勫瓧绗︿覆涓瓧鑺傜殑涓暟銆傚鏋滆繖涓�间负-1锛屽瓧绗︿覆灏嗚璁惧畾涓轰互NULL涓虹粨鏉熺鐨勫瓧绗︿覆锛屽苟涓旇嚜鍔ㄨ绠楅暱搴︺��
lpWideCharStr锛氭寚鍚戞帴鏀惰杞崲瀛楃涓茬殑缂撳啿鍖恒��
cchWideChar锛氭寚瀹氱敱鍙傛暟lpWideCharStr鎸囧悜鐨勭紦鍐插尯鐨勫瓧鑺備釜鏁般�傝嫢姝ゅ�间负闆讹紝鍑芥暟杩斿洖缂撳啿鍖烘墍蹇呴渶鐨勫瀛楃鏁帮紝鍦ㄨ繖绉嶆儏鍐典笅锛宭pWideCharStr涓殑缂撳啿鍖轰笉琚娇鐢ㄣ��
杩斿洖鍊�锛氬鏋滃嚱鏁拌繍琛屾垚鍔燂紝骞朵笖cchWideChar涓嶄负闆讹紝杩斿洖鍊兼槸鐢眑pWideCharStr鎸囧悜鐨勭紦鍐插尯涓啓鍏ョ殑瀹藉瓧绗︽暟锛涘鏋滃嚱鏁拌繍琛屾垚鍔燂紝骞朵笖cchMultiByte涓洪浂锛岃繑鍥炲�兼槸鎺ユ敹鍒板緟杞崲瀛楃涓茬殑缂撳啿鍖烘墍闇�姹傜殑瀹藉瓧绗�鏁板ぇ灏忋�傚鏋滃嚱鏁拌繍琛屽け璐ワ紝杩斿洖鍊间负闆躲��
鍑芥暟鍘熷瀷
int WideCharToMultiByte(
UINT CodePage,
DWORD dwFlags,
LPCWSTR lpWideCharStr,
int cchWideChar,
LPSTR lpMultiByteStr,
int cbMultiByte,
LPCSTR lpDefaultChar,
LPBOOL lpUsedDefaultChar);
鍑芥暟鍔熻兘聽
姝ゅ嚱鏁版妸瀹藉瓧绗︿覆杞崲鎴愭寚瀹氱殑鏂扮殑瀛楃涓诧紝濡侫NSI锛孶TF8绛夛紝鏂板瓧绗︿覆涓嶅繀鏄瀛楄妭瀛楃闆嗐�偮�
鍙傛暟
lpWideCharStr 锛� 寰呰浆鎹㈢殑瀹藉瓧绗︿覆銆�
cchWideChar 锛� 寰呰浆鎹㈠瀛楃涓茬殑闀垮害锛�-1琛ㄧず杞崲鍒板瓧绗︿覆缁撳熬銆�
lpMultiByteStr 锛� 鎺ユ敹杞崲鍚庤緭鍑烘柊涓茬殑缂撳啿鍖恒��
cbMultiByte 锛氳緭鍑虹紦鍐插尯澶у皬锛屽鏋滀负0锛宭pMultiByteStr灏嗚蹇界暐锛屽嚱鏁板皢杩斿洖鎵�闇�缂撳啿鍖哄ぇ灏忚�屼笉浣跨敤lpMultiByteStr銆�
lpDefaultChar 锛� 鎸囧悜瀛楃鐨勬寚閽堬紝 鍦ㄦ寚瀹氱紪鐮侀噷鎵句笉鍒扮浉搴斿瓧绗︽椂浣跨敤姝ゅ瓧绗︿綔涓洪粯璁ゅ瓧绗︿唬鏇裤�傚鏋滀负NULL鍒欎娇鐢ㄧ郴缁熼粯璁ゅ瓧绗︺�傚浜庤姹傛鍙傛暟涓篘ULL鐨刣wFlags鑰屼娇鐢ㄦ鍙傛暟锛屽嚱鏁板皢澶辫触杩斿洖骞惰缃敊璇爜ERROR_INVALID_PARAMETER銆�
lpUsedDefaultChar 锛氬紑鍏冲彉閲忕殑鎸囬拡锛岀敤浠ヨ〃鏄庢槸鍚︿娇鐢ㄨ繃榛樿瀛楃銆傚浜庤姹傛鍙傛暟涓篘ULL鐨刣wFlags鑰屼娇鐢ㄦ鍙傛暟锛屽嚱鏁板皢澶辫触杩斿洖骞惰缃敊璇爜ERROR_INVALID_PARAMETER銆俵pDefaultChar鍜宭pUsedDefaultChar閮借涓篘ULL锛屽嚱鏁颁細鏇村揩涓�浜涖��
杩斿洖鍊� 锛氬鏋滃嚱鏁版垚鍔燂紝涓攃bMultiByte闈�0锛岃繑鍥炲啓鍏pMultiByteStr鐨勫瓧鑺傛暟(鍖呮嫭瀛楃涓茬粨灏剧殑null)锛沜bMultiByte涓�0锛屽垯杩斿洖杞崲鎵�闇�瀛楄妭鏁�銆傚嚱鏁板け璐ワ紝杩斿洖0銆�
涓嬮潰鏄畝鍗曠殑鍑芥暟瀹炵幇
void utf8ToGbk(char *utf8String, char *gbkString)
{
wchar_t *unicodeStr = NULL;
int nRetLen = 0;
nRetLen = MultiByteToWideChar(CP_UTF8, 0, utf8String, -1, NULL, 0);
//姹傞渶姹傜殑瀹藉瓧绗︽暟澶у皬
unicodeStr = (wchar_t *)malloc(nRetLen * sizeof(wchar_t));
nRetLen = MultiByteToWideChar(CP_UTF8, 0, utf8String, -1, unicodeStr, nRetLen);
//灏唘tf-8缂栫爜杞崲鎴恥nicode缂栫爜
nRetLen = WideCharToMultiByte(CP_ACP, 0, unicodeStr, -1, NULL, 0, NULL, 0);
//姹傝浆鎹㈡墍闇�瀛楄妭鏁�
nRetLen = WideCharToMultiByte(CP_ACP, 0, unicodeStr, -1, gbkString, nRetLen, NULL, 0);
//unicode缂栫爜杞崲鎴恎bk缂栫爜
free(unicodeStr);
}
绗笁绡囨枃绔犲搱鍝堝搱鍝�
浜虹敓鏄竴鍦鸿妯″簽澶х殑鎽稿僵娓告垙锛屽彧鏈変腑濂栫殑褰╃エ灞曠幇鍦ㄦ垜浠溂鍓嶃�傗�斺�斻�婄焊鐗岀殑绉樺瘑銆�
缁欐垜鐐瑰崄涓禐鎴戝氨涔版澂鍙箰搴嗙涓�