windows下源文件编码问题

在windows下不要直接右击桌面创建.txt再改成.c,这种方式容易引起编码问题

windows下gvim的设置:

先打开gvim再用:w newfile.c这种方式来创建新文件

vimrc文档中设置保存文件的编码方式为chinese或gbk

  • set fileencoding=chinese //gbk也可以

char与wchar_t

  • 英文字符在char中占一位,汉字字符在char中占两位;

    char采用可变长编码(具体取决于编译器),与printf配合使用

  • 英文字符和中文字符在wchat_t中统一占两位.

    wchat_t采用unicode编码,与wprintf配合使用

    在字符字面量前统一加L

编码范围

英文字符(ascii, 含标点,字母,数字): 0x00-0x7F,每个英文字符占一个字节(char)

汉字字符(GB2312, 含汉字及标点): 0xA1A1(41377) - 0xFEFE(65278), 每个汉字字符占两个字节

汉字范围: 0xB0A1(45217) - 0xF7FE(63486)

注: wchat_t使用unicode编码,每个字符占两个字节,中文范围是4E00-9FBF, unicode里面包含了各个国家语言文字的编码,而GB2312是汉字的一个专有编码集

内置类型判断函数

#include <stdio.h>
#include <ctype.h>
#include <limits.h> static void prclass(const char *name,int (*fn)(int)){
int c; fputs(name,stdout);
fputs(": ",stdout);
for(c=EOF;c <= UCHAR_MAX;++c)
if((*fn)(c))
fputc(c,stdout);
fputs("\n",stdout);
} int main(int argc,char *argv[]){
prclass("ispunct",&ispunct);
prclass("isdigit",&isdigit);
prclass("islower",&islower);
prclass("isupper",&isupper);
prclass("isalpha",&isalpha);
prclass("isalnum",&isalnum);
return 0;
}

打印汉字

需配合setlocale使用, ".936"代表GBK编码,以下三选一

setlocale(LC_ALL,"");

setlocale(LC_ALL, "chinese-simplified" )

setlocale(LC_CTYPE,".936");

#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <locale.h> int main(int argc,char *argv[]){
setlocale(LC_CTYPE,".936");
wchar_t str[]=L"我是abc";
wprintf(L"%s\n",str); //printf与char*也是可以输出中文的
return 0;
}

英文与汉字区分

判断一个数组中的字符为汉字字符或英文字符时,先以unsigned char方式检查char[0],

如果char[0] <= 7F则判定为英文字符,如果char[0]>= A1 && char[1]>=A1则判定为中文字符,更完整的应该是把上限也判断一下

#include <stdlib.h>
#include <stdio.h> int word_test(const unsigned char *s){
if(s[0] <= 0x7F){
printf("en ");
return 0;
}
else if((s[0] >= 0xA1) && (s[1] >= 0xA1)){
printf("zh-cn ");
return 1;
}
else{
printf("unknown ");
return -1;
}
} void sentence_test(const unsigned char *s){
int i=0,ret;
while(s[i] != '\0'){
ret=word_test(&s[i]);
if(ret == 0)
i++;
else if(ret == 1)
i+=2;
else
exit(-1);
}
printf("[count of char=%d\n",i);
}
int main(int argc, const char *argv[]){
word_test("我");
puts("");
word_test("a");
puts("");
sentence_test("w我是abc");
return 0;
}

统计文件中的字符数

运行时输入绝对路径,

#include <stdio.h>
#include <ctype.h>
#include <assert.h> int main(int argc,char *argv[]){
FILE *fp=fopen(argv[1],"r");
assert(fp != NULL); int alpha=0,num=0,punc=0;
char c;
while((c=fgetc(fp)) != EOF){
if(isalpha(c))
alpha++;
if(isdigit(c))
num++;
if(ispunct(c))
punc++;
}
printf("count of alpha=%d\n",alpha);
printf("count of num=%d\n",num);
printf("count of punc=%d\n",punc);
return 0;
}

GB2312获取汉字首字母

#include <stdlib.h>
#include <locale.h>
#include <stdio.h>
#include <string.h> #define CONVERT(start, end, code, letter) if(code >= start && code <= end) return letter
char Convert(wchar_t n){
// 根据汉字区域码获取拼音声母GB2312,不适用于wchar_t(unicode)
CONVERT(0xB0A1, 0xB0C4, n, 'A');
CONVERT(0XB0C5, 0XB2C0, n, 'B');
CONVERT(0xB2C1, 0xB4ED, n, 'C');
CONVERT(0xB4EE, 0xB6E9, n, 'D');
CONVERT(0xB6EA, 0xB7A1, n, 'E');
CONVERT(0xB7A2, 0xB8C0, n, 'F');
CONVERT(0xB8C1, 0xB9FD, n, 'G');
CONVERT(0xB9FE, 0xBBF6, n, 'H');
CONVERT(0xBBF7, 0xBFA5, n, 'J');
CONVERT(0xBFA6, 0xC0AB, n, 'K');
CONVERT(0xC0AC, 0xC2E7, n, 'L');
CONVERT(0xC2E8, 0xC4C2, n, 'M');
CONVERT(0xC4C3, 0xC5B5, n, 'N');
CONVERT(0xC5B6, 0xC5BD, n, 'O');
CONVERT(0xC5BE, 0xC6D9, n, 'P');
CONVERT(0xC6DA, 0xC8BA, n, 'Q');
CONVERT(0xC8BB, 0xC8F5, n, 'R');
CONVERT(0xC8F6, 0xCBF9, n, 'S');
CONVERT(0xCBFA, 0xCDD9, n, 'T');
CONVERT(0xCDDA, 0xCEF3, n, 'W');
CONVERT(0xCEF4, 0xD1B8, n, 'X');
CONVERT(0xD1B9, 0xD4D0, n, 'Y');
CONVERT(0xD4D1, 0xD7F9, n, 'Z');
return '\0';
} void py_Invert(const char *src,char *ret,int size){
const char *sChinese = src; wchar_t wchr = 0;
int nCount = strlen(sChinese) / 2; int i,j;
char rst;
for (i = 0, j = 0; i < nCount,i<size-1; ++i){
wchr = (sChinese[j++] & 0xff) << 8; // 高字节
wchr |= (sChinese[j++] & 0xff); // 低字节 ret[i]=Convert(wchr);
}
ret[i]=0;
}
int main(int argc,char *argv[]){
setlocale(LC_CTYPE,".936");
char str[]="这是测试";
char ret[10]={0};
py_Invert(str,ret,10);
printf("%s\n",ret);
return 0;
}

以GB2312编码打印所有汉字

#include <stdlib.h>
#include <locale.h>
#include <stdio.h> int main(int argc,char *argv[]){
setlocale(LC_CTYPE,".936");
char test1[3]={0xB0,0xA1,0}; unsigned char index;
for(index=0xA1;index<=0xFF;index++){
test1[1]=index;
printf("%s ",test1);
if(index == 0xFF)
test1[0]=(unsigned char)test1[0]+1;
if(((unsigned char)test1[0] == 0xD7) && ((unsigned char)test1[1] == 0xF9))
break;
}
return 0;
}

类型(type)判断的更多相关文章

  1. Logstash type来标记事件类型,通过type判断

    /*************** 根据type判断 input { file { type => "zj_frontend_access" path => [" ...

  2. 关于javascript 里面类型的判断

    javacript至今共有7中类型 Six data types that are primitives: Boolean Null Undefined Number String Symbol (n ...

  3. web前端对上传的文件进行类型大小判断的js自定义函数

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. javascript 类型的判断

    在平常写js代码,类型判断必不可少,那么我们常见有哪几种?看到了标题,先不看你会想到那些方法 ,常用呢些呢?那么今天我自己总结一些判断类型的判断,如有错,万望告知! 1:typeof 常用这种方法不错 ...

  5. orcle自定义类型type/create or replace type

    一.type / create or repalce type 区别联系 相同: 可用关键字create type 或者直接用type定义自定义类型, 区别: create type 变量 as ta ...

  6. #定义一个方法get_num(num),num参数是列表类型,判断列表里面的元素为数字类型。其他类型则报错,并且返回一个偶数列表:(注:列表里面的元素为偶数)。

    #定义一个方法get_num(num),num参数是列表类型,判断列表里面的元素为数字类型.其他类型则报错,并且返回一个偶数列表:(注:列表里面的元素为偶数). def get_num(num): i ...

  7. jquery中关于对象类型的判断原理

    class2type[ core_toString.call(obj) ] jquery中关于对象类型的判断原理 jquery源码中关于类型判断的工具函数为type,调用方法为$.type()或者jQ ...

  8. 转载:oracle 自定义类型 type / create type

    标签:type create oracle object record 一:Oracle中的类型有很多种,主要可以分为以下几类: 1.字符串类型.如:char.nchar.varchar2.nvarc ...

  9. oracle 自定义类型 type / create type

    一:Oracle中的类型有很多种,主要可以分为以下几类: 1.字符串类型.如:char.nchar.varchar2.nvarchar2. 2.数值类型.如:int.number(p,s).integ ...

随机推荐

  1. Python2中range 和xrange的区别??

    两者用法相同,不同的是range返回的结果是一个列表,而xrange的结果是一个生成器, 前者是直接开辟一块内存空间来保存列表,后者是边循环边使用,只有使用时才会开辟内存空间, 所以当列表很长时,使用 ...

  2. IDEA错误: 找不到或无法加载主类 com.xxx.freight.dofreight.doFreight解决办法

    1.右键点击工程,选择open Module Settings或点击File选择Project Structure,进入页面 2.选择Artifacts->JAR->From module ...

  3. wxpython程序基本功能源码整理,包括基本文字,输入框,字体设置,按钮绑定事件触发

    #coding=utf-8 import wx class MyApp(wx.App): def __init__(self): wx.App.__init__(self) def OnInit(se ...

  4. vue 中使用style(样式)

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. Mac更改PHP默认目录的方法

    参考:http://www.cnblogs.com/muyunlee/p/6386095.html

  6. Route的exact属性

    exact是Route下的一个属性,react路由会匹配到所有能匹配到的路由组件,exact能够使得路由的匹配更严格一些. exact的值为bool型,为true时表示严格匹配,为false时为正常匹 ...

  7. linux安装 rsync 客户端和相关权限认证

    [root@rsync-client-inotify /]# yum install rsync -y [root@rsync-client-inotify /]# echo "redhat ...

  8. Sql server 启用调试

    在SQL Server 2008管理平台上,调试2005的数据库,会报错. 用 SQL Server 2008管理平台,调试本机数据库,当登录服务器名为“.”的时候也会报错.   解决方法,暂时使用S ...

  9. OGG-01169

    OGG-01169  Oracle GoldenGate Delivery for Oracle, dwarer.prm:  Encountered an update where all key c ...

  10. CondaHTTPError: HTTP 000 CONNECTION FAILED for url <https://mirrors.ustc.edu.cn/anaconda/pkg

    conda安装时一直报错,换源什么的都不好使,折腾了半天,直到看到https://blog.csdn.net/u013383596/article/details/87718472 将https改为h ...