#include<stdio.h>
#include<windows.h>
void GetUtf8(unsigned short uni)
{
unsigned char utf_8[3];
utf_8[0]=(0xe<<4) | ((uni & 0xf000)>>12);//相当于把二进制1110右移12位
utf_8[1]=(0x2<<6) | ((uni & 0x0fc0)>>6); //相当于把二进制10右移16位
utf_8[2]=(0x2<<6) | (uni & 0x003f); //相当于取二进制低6位
printf("UTF-8编码的第一个字节为0x%.2x\n",utf_8[0]);
printf("UTF-8编码的第一个字节为0x%.2x\n",utf_8[1]);
printf("UTF-8编码的第一个字节为0x%.2x\n",utf_8[2]);
}
void main()
{
char a[3];
wchar_t str;
printf("输入一个汉字:\n");
gets(a);
MultiByteToWideChar(CP_ACP, 0, a, -1, &str, 1);
printf("%s", a);
printf("字Unicode编码为%x\n",(unsigned short)str);
GetUtf8((unsigned short)str);
}
温馨提示:答案为网友推荐,仅供参考