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. python学习第四十六天dir( )函数用法

    dir( )函数有点像目录的意思,但是他是包含由模块定义的名称的字符串的排序列表.这个列表包含模块中定义的所有模块,变量和函数的名称. 列举其用法 import time content = dir( ...

  2. C Yuhao and a Parenthesis

    Yuhao and a Parenthesis time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  3. 利用ssh-copy-id实现SSH无密码登录

    第一步: 在远程服务器产生公钥与私钥对: $ ssh-keygen -t rsa 按照提示输入完后,会在~/.ssh目录下生成id_rsa和id_rsa.pub这两个文件 第二步:用ssh-copy- ...

  4. spark复习笔记(5):API分析

    0.spark是基于hadoop的mr模型,扩展了MR,高效实用MR模型,内存型集群计算,提高了app处理速度. 1.特点:(1)在内存中存储中间结果 (2)支持多种语言:java scala pyt ...

  5. vue中的scope

    在vue文件中的style标签上,有一个特殊的属性:scoped. 当一个style标签拥有scoped属性时,它的CSS样式就只能作用于当前的组件,也就是说,该样式只能适用于当前组件元素. 通过该属 ...

  6. Array数组的使用

    public class ArrayDemo { public static void main(String[] args) { int[] arr = {13,44,55,667,67,78}; ...

  7. 树——minimum-depth-of-binary-tree(二叉树的最小深度)

    问题: Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the s ...

  8. POJ 2104 区间第k大(主席树)

    题目链接:http://poj.org/problem?id=2104 题目大意:给定还有n个数的序列,m个操作,每个操作含有l,r,k,求区间[l,r]第k大 解题思路:线段树只能维护序列的最大值最 ...

  9. spring requestbody json

    1  @requestbody string param 前台将jsonobject序列化成字符串 后台解析成JsonObject 2 @requestbody map<string,objec ...

  10. 21.Nodejs基础知识(下)——2019年12月16日

    2019年10月04日16:56:23 7. 模块 7.1 暴露一个类,字段 var bar = require("./bar.js"); var msg = "你好&q ...