標籤:編碼轉換 unicode utf8 c-c++
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;}
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;}
c/c++ 中文字串轉Unicode和UTF8