mmxingye

导航

01 | windows下的字符转换

#include <stdio.h>
#include <WinSock2.h>
#include <windows.h>

//gbk -> utf-16
int test1() {
	char text_gbk[256] = "砂隐村"; //字符串字面常量,取决于cpp文件自身的
	wchar_t text_utf16[256] = {};
	int n = MultiByteToWideChar(CP_ACP, 0,	//cp_acp 当前代码页
		text_gbk, strlen(text_gbk),
		text_utf16, 256);
	printf("结果:%d个宽字符\n", n);
	return 0;
}
//gbk -> utf-16 ->utf-8
int test2() {
	char text_gbk[] = "木叶村";
	wchar_t text_utf16[256] = { 0 };
	int n1 = MultiByteToWideChar(CP_ACP, 0,
		text_gbk, strlen(text_gbk),
		text_utf16, 256);
	char text_utf8[256] = { 0 };
	int n2 = WideCharToMultiByte(CP_UTF8, 0,
		text_utf16, n1,
		text_utf8, 256,
		NULL,0);
	printf("结果:%d个字节\n", n2);
	return 0;
}

int main() {
	
	test1();
	test2();
	return 0;
}

posted on 2022-04-09 21:28  独立树  阅读(41)  评论(0编辑  收藏  举报