版权声明:可以转载奥 https://blog.csdn.net/Jiajikang_jjk/article/details/87922310
文章目录
【C语言】获取中文字符(汉字)的16进制
一、前言
内容过于真实,简单,仅作为博主此后方便查阅。
二、代码
/*
* @Author: jjk
* @Date: 2019-02-25 19:29:51
* @Last Modified by: jjk
* @Last Modified time: 2019-02-25 19:32:59
* 测试十六进制
*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i;
char unsigned ch[7] = {"贾继康"};
char ch2[5];
char unsigned ch3[3] = {"。"};
printf("汉字:“贾继康” 的十六进制:");
for (i = 0; i < 6; i++)
{
printf(" %#x", ch[i]); // %#x是带格式输出, 效果为在输出前加0x;%x是16进制输出.
}
// -16 -29 表示汉字:你
// 具体测试:https://blog.csdn.net/Jiajikang_jjk/article/details/86775249
ch2[0] = -68;
ch2[1] = -42;
printf("\n\n十进制数-68 -42 表示汉字:%c%c\n", ch2[0], ch2[1]);
// >B0 >A0 中文的范围
//<F7 <FF
//0xa1 0x40 符号
//0xa9 0xff 符号
// printf("jjk%c%c:", 0xa1, 0xb0);
printf("\n。的十六进制是:%#x %#x \n", ch3[0], ch3[1]);
i = 0;
int j, tem = 0;
char unsigned ch4[] = {"、。。?!:你好hello"};
printf("这句话中有多少个中文符号:%s", ch4);
while (ch4[i] != '\0') // 未到句尾
{
j = i + 1;
if (j == '\0') // 到了句尾就结束
{
break;
}
else if (ch4[i] <= 0xa9 && ch4[i] >= 0xa1) // 中文符号
{
if (ch4[j] >= 0x40 && ch4[j] <= 0xff) // 中文符号
{
tem++;
}
i = i + 2;
}
else
{
i++; // 非中文字符时,向前移一位
}
}
printf("\n中文标点符号有%d个", tem);
return 0;
}