最近编写程序,中文乱码问题让很多同学很头疼,那么今天黄老师来帮大家剖析一下:
以 “好人“ 两个字为例:
等会我们使用代码跟踪内存,来查看字节的编码:
先上代码:
#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
char * ConvertUTF8ToANSI(char *utf8 ) {
int count = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, NULL, 0);
int len = sizeof(wchar_t)* (count + 1);
wchar_t* wide = malloc(len);
memset(wide, 0, len);
MultiByteToWideChar(CP_UTF8, 0, utf8, -1, wide, count);
count = WideCharToMultiByte(CP_ACP, 0, wide, -1, NULL, 0, NULL, NULL);
int len2 = sizeof(char) *(count + 1);
char* ansi = malloc(len2 );
memset(ansi, 0, len2);
WideCharToMultiByte(CP_ACP, 0, wide, -1, ansi, count, NULL, NULL);
free(wide);
return ansi;
}
voi