c/c++ 中文字符串轉Unicode和UTF8
1. 描述
在windows上做系統編程,少不了會遇到處理中文字符串的問題。而大多時候中文漢字都是以多字節編碼的方式展現的。為了實現更好的兼容性或一些特殊的需求,(比如在網頁上顯示。)常需要將其轉換成unicode或者utf8的格式。
2. 代碼示例
2.1 中文字符串轉Unicode
/** int CN2Unicode(char input,wchar_t output)功能:中文字符轉換為unicode字符 參數:input,包含中文的字符串,output,Unicode字符串 */ int CN2Unicode(char input,wchar_t output) { int len = strlen(input);//wchar_t *out = (wchar_t *) malloc(len*sizeof(wchar_t)); len=MultiByteToWideChar(CP_ACP,0,input,-1,output,MAX_PATH); return 1;
}</pre>
2.2 中文字符串轉utf8
/** int CN2Utf8(char input,char output)功能:中文字符串轉換為utf8字符串 參數:input,包含中文的字符串,output,utf8字符串 **/ int CN2Utf8(char input,char output) { int len ; wchar_t out = (wchar_t ) malloc(len*sizeof(wchar_t));len = MultiByteToWideChar(CP_ACP,0,input,-1,out,strlen(input)+1); WideCharToMultiByte(CP_UTF8,0,out,wcslen(out),output,len,NULL,NULL); return 1;
}</pre>
本文由用戶 dgy7 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!