计算中文+字母混合的长度
char *str="Windows 7 操作系统";
用 strlen(str) 只返回字节数 18 ,但我要反回字符数(包括空格):14
#include "stdio.h"
void main()
{
int ennum=0,chnum=0;//英文字母计数和汉字计数
char *str="Windows 7 操作系统";
int i;
for(i=0;str;i++)
{
if(('A'<=str&&'Z'>=str)||('a'<=str&&'z'>=str))
ennum++;
if(str<0) chnum++;
printf("英文字母数为:",ennum);
printf("中文字数为:",chnum/2);//这个很关键的,因为两个字节表示一个汉字
}
}
计算长度
char str[] = "some srting";
int length = sizeof(str) / sizeof(str[0]);
int length = strlen(str);
显示汉字
h文件
#ifndef __HANZHI_H
#define__HANZHI_H
#include "STM32f10x.h"
#include "lcd.h"
extern void hanz32(u16 x,u16 y,char *Buffer,u16 Color); //¿í¡á¸ß£¨ÏñËØ£©: 32¡á32oo×Ö
#endif
c文件
#include "hanzhi.h"
#include
#include
#include
typedef struct zm //oo×Ö×ÖÄ£êy¾Y½á11
{
char neima[2]; //oo×ÖÄúÂë2×Ö½ú
unsigned char zhimo[128]; //oo×ÖμãÕó
} hanzhimo;
const hanzhimo hz32[]={ //¿í¡á¸ß£¨ÏñËØ£©: 32¡á32oo×Ö
{{"陈"},{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x20,0x00,0x10,0x20,0x38,0x00,
0x1F,0xF0,0x30,0x00,0x18,0x60,0x60,0x10,0x18,0x60,0x60,0x10,0x18,0x5F,0xFF,0xF8,
0x18,0xC0,0xC0,0x00,0x18,0x80,0xC0,0x00,0x18,0x81,0x88,0x00,0x18,0x81,0x8C,0x00,
0x19,0x01,0x0C,0x00,0x19,0x03,0x0C,0x00,0x18,0x82,0x0C,0x00,0x18,0x46,0x0C,0x18,
0x18,0x6F,0xFF,0xFC,0x18,0x24,0x0C,0x00,0x18,0x30,0x0C,0x00,0x18,0x30,0x8C,0x00,
0x18,0x31,0xCC,0x80,0x1E,0x61,0x8C,0x40,0x19,0xE3,0x0C,0x30,0x18,0xC6,0x0C,0x38,
0x18,0x04,0x0C,0x18,0x18,0x0C,0x0C,0x0C,0x18,0x10,0x0C,0x0C,0x18,0x20,0x0C,0x00,
0x18,0x40,0xFC,0x00,0x18,0x00,0x3C,0x00,0x18,0x00,0x10,0x00,0x00,0x00,0x00,0x00,
}},
{{"添"},{
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x00,0x00,0x40,
0x06,0x00,0x00,0xE0,0x06,0x1F,0xFF,0x00,0x03,0x00,0xC0,0x00,0x02,0x00,0x80,0x00,
0x00,0x01,0x80,0x00,0x00,0x01,0x80,0x30,0x60,0x7F,0xFF,0xF8,0x31,0x83,0x08,0x00,
0x19,0x03,0x04,0x00,0x19,0x06,0x06,0x00,0x09,0x06,0x03,0x00,0x0A,0x0C,0x81,0x80,
0x02,0x18,0xE0,0xE0,0x02,0x30,0xC0,0x78,0x02,0x60,0xC0,0x3C,0x04,0xC0,0xC0,0x08,
0x05,0x00,0xC0,0x00,0x7E,0x10,0xC8,0xC0,0x1C,0x10,0xCC,0x60,0x0C,0x10,0xC4,0x60,
0x0C,0x30,0xC6,0x30,0x0C,0x60,0xC6,0x30,0x0C,0x60,0xC2,0x30,0x1C,0x00,0xC0,0x00,
0x0C,0x0F,0xC0,0x00,0x00,0x03,0xC0,0x00,0x00,0x01,0x80,0x00,0x00,0x00,0x00,0x00,
}},
{{"富"},{
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02,0x00,0x00,0x00,0x01,0x80,0x00,
0x00,0x01,0x80,0x00,0x04,0x00,0x80,0x10,0x07,0xFF,0xFF,0xF8,0x0C,0x00,0x00,0x38,
0x0C,0x00,0x00,0x20,0x18,0x00,0x03,0x40,0x00,0xFF,0xFC,0x80,0x00,0x00,0x00,0x00,
0x00,0x40,0x02,0x00,0x00,0x7F,0xFE,0x00,0x00,0x60,0x06,0x00,0x00,0x60,0x06,0x00,
0x00,0x60,0x06,0x00,0x00,0x7F,0xFE,0x00,0x00,0x60,0x00,0x00,0x02,0x00,0x00,0x80,
0x01,0xFF,0xFF,0xC0,0x01,0x01,0x80,0x80,0x01,0x01,0x80,0x80,0x01,0x01,0x80,0x80,
0x01,0x01,0x80,0x80,0x01,0xFF,0xFF,0x80,0x01,0x01,0x80,0x80,0x01,0x01,0x80,0x80,
0x01,0x01,0x80,0x80,0x01,0xFE,0x7F,0x80,0x01,0x00,0x00,0x80,0x00,0x00,0x00,0x00,
}}
};
void hanz32(u16 x,u16 y,char *Buffer,u16 Color)//¿í¡á¸ß£¨ÏñËØ£©: 32¡á32oo×Ö
{
u8 i=0,j,k,m,n=0,c1,c2,L;
u32 tmp_char=0,tmp_char0=0,tmp_char1=0,tmp_char2=0,tmp_char3=0;
L=strlen(Buffer);
//printf("rn The current AD value = %d V rn",n);
while(n
{
c1=Buffer;c2=Buffer[i+1];
for(j=0;j
{
if(c1==hz32[j].neima[0]&&c2==hz32[j].neima[1])
{
break;
}
}
for (m=0;m<32;m++)
{
tmp_char0=hz32[j].zhimo[4*m];
tmp_char0=(tmp_char0<<24);
tmp_char1=hz32[j].zhimo[4*m+1];
tmp_char1=(tmp_char1<<16);
tmp_char2=hz32[j].zhimo[4*m+2];
tmp_char2=(tmp_char2<<8);
tmp_char3=hz32[j].zhimo[4*m+3];
tmp_char=tmp_char0|tmp_char1|tmp_char2|tmp_char3;
for (k=0;k<32;k++)
{
if (tmp_char & 0x80000000)
{
LCD_ColorPoint(x+k,y+m,Color);
}
else
{
//LCD_ColorPoint(x+16-i,y+j,bkColor);
}
tmp_char <<= 1;
}
}
x=x+32;
i=i+2;
n=n+2;
}
}
main文件
LCD_Init();
hanz32(22,22,"陈添富", RED); //32X32点阵文字
|