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,Ansi转Unicode,Ansi文件转Utf8文件
有时候需要把ansi文件内容转换为utf8编码,读取一行之后,把ansi字符串转换为utf8,之后写入文件。
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <windows.h> #include <assert.h>
char* Unicode2Utf8(const char* unicode) { int len; len = WideCharToMultiByte(CP_UTF8, 0, (const wchar_t*)unicode, -1, NULL, 0, NULL, NULL); char *szUtf8 = (char*)malloc(len + 1); memset(szUtf8, 0, len + 1); WideCharToMultiByte(CP_UTF8, 0, (const wchar_t*)unicode, -1, szUtf8, len, NULL,NULL); return szUtf8; }
char* Ansi2Unicode(const char* str) { int dwUnicodeLen = MultiByteToWideChar(CP_ACP,0,str,-1,NULL,0); if(!dwUnicodeLen) { return strdup(str); } size_t num = dwUnicodeLen*sizeof(wchar_t); wchar_t *pwText = (wchar_t*)malloc(num); memset(pwText,0,num); MultiByteToWideChar(CP_ACP,0,str,-1,pwText,dwUnicodeLen); return (char*)pwText; }
char* ConvertAnsiToUtf8(const char* str) { char* unicode = Ansi2Unicode(str); char* utf8 = Unicode2Utf8(unicode); free(unicode); return utf8; }
int main(int argc, char *argv[]) { printf("Hello, world\n"); //1.构造一个ansi文件,内容是"中文abc",看hex编码. //ansi: D6 D0 CE C4 61 62 63 //utf8: E4 B8 AD E6 96 87 61 62 63 char ansi[] = {0xD6,0xD0,0xCE,0xC4,0x61,0x62,0x63,0}; char utf8[] = {0xE4,0xB8,0xAD,0xE6,0x96,0x87,0x61,0x62,0x63,0}; char* str = ConvertAnsiToUtf8(ansi); assert(!strcmp(str,utf8)); free(str); return 0; }
|