如鹏网.Net基础2 专题课:ASCII码和拆数

第 1 节 ASCII介绍和char转换为int

美国标准信息交换码

计算机中存储的都是二进制数据。

什么是ASCII码?

char和int的转换;

char的大写小转换;

‘1’转换为1;和1转换为‘1’

0  空字符

8 退格(Backspace)

9 Tab

13 回车

32 空格

48  0-9 

65 A-Z

97 a-z

char[0,65535]

//把char转换为int(可隐式转换)

char和int各种转换
 
namespace ConsoleApplication7
{
    class Program
    {
        static void Main(string[] args)
        {
            /*
            Console.WriteLine(int.MaxValue);
            Console.WriteLine(int.MinValue);
 
            Console.WriteLine((int)char.MaxValue);//65535,显式转换,以便于匹配WriteLine的int类型的重载
            Console.WriteLine((int)char.MinValue);//0
 
            char c='a';
            Console.WriteLine(c);
            int i = c;//把char隐式转换为int(int范围比char大)
            //只要把char数据隐式或者显式转换为int,得到的int就是char的ASCII码
            Console.WriteLine(i);
            */
 
            /*
            char c = (char)97;
            Console.WriteLine(c);
 
            int i = 98;
            char c1 = (char)i;
            Console.WriteLine(c1);*/
 
            /*
            //'1'和1的区别
            char c1 = '1';//49
            int i1 = 1;//1
            int i2 = c1;
            Console.WriteLine(i2);
            Console.WriteLine(c1==i1);
            */
 
            /*
            char c1 = '0';
            char c2 = (char)(c1 + 2);//不能(char)c1+1
            Console.WriteLine(c2);
 
            char c3 = '5';
            char c4 = (char)(c3-2);
            Console.WriteLine(c4);
            */
 
            //Console.WriteLine(intToChar(5));
 
            //Console.WriteLine(charToInt('3'));
            Console.WriteLine(toUpper('d'));
 
            Console.ReadKey();
        }
 
        /// <summary>
        /// 把1转换为'1',2转换为'2'
        /// </summary>
        /// <param name="i"></param>
        /// <returns></returns>        
        static char intToChar(int i)
        {
            if (i < 0 || i > 9)
            {
                throw new Exception("i必须在0-9之间");
            }
            return (char)('0' + i);//i=3
        }
 
        /// <summary>
        /// 把'1'转换为1
        /// </summary>
        /// <param name="c"></param>
        /// <returns></returns>
        static int charToInt(char c)
        {
            //'0','1'....'9'
            if (c < '0' || c > '9')//判断是否是数字字符
            {
                throw new Exception("不是合法的数字字符");
            }
            return c - '0';
        }
 
        //'a'→'A'
        static char toUpper(char c)
        {
            if (c < 'a' || c > 'z')
            {
                throw new Exception("不是小写字符");
            }
            //'a':97,'A':65;'c':99,'C':67
            //研究发现小写字符比大写字符大32
            //return c
            //return (char)(c - 32);
            return (char)(c-('a'-'A'));
        }
 
        //任务:写一个转换为小写的方法
    }
}

 

------------------------------------------------
第 2 节 char的加减法

------------------------------------------------
第 3 节 各种转换

------------------------------------------------
第 4 节 取得一个数各个位的值

  class Program
    {
        static void Main(string[] args)
        {
            /*
            int i = 365;
            //就是三位数
 
            int sheng = 365;
            int ge = sheng % 10;
 
           // int ge = i % 10;
            Console.WriteLine("个位"+ge);
            //int sheng = i / 10;//36
            sheng = sheng / 10;
            Console.WriteLine("剩" + sheng);
 
            int shi = sheng % 10;
            Console.WriteLine("十位" + shi);
 
            sheng = sheng / 10;
            Console.WriteLine("剩" + sheng);
 
            int bai = sheng % 10;
            Console.WriteLine("百位"+bai);
            sheng = sheng / 10;
            Console.WriteLine("剩" + sheng);
            */
 
            /*
            int n = 3721886;
            int sheng = n;
            while (sheng != 0)
            {
                int wei = sheng % 10;
                Console.WriteLine(wei);
 
                sheng = sheng / 10;
            }*/
 
            int n = 3721886;
            string s = n.ToString();//项目中哪个方便用哪个。面试的时候尽量不用.net内置的方法
            for (int i = 0; i < s.Length; i++)
            {
                char ch = s[i];
                int iWei = ch-'0';//'1'→1
                //Console.WriteLine(ch);
                Console.WriteLine(iWei);
            }
            Console.ReadKey();
        }
    }

 

------------------------------------------------
第 5 节 自测题

1) 将给定的金额转换为中文,比如输入1235,则返回"壹贰叁伍"。

2) 数字加千分位:要求用户输入一个整数,编写一个方法,方法将整数转换为一个从低位开始每三位一个逗号的“千分位”字符串表示形式。比如用户输入1123556,则返回”1,123,556”

3) 输出1-999中能被3整除,而且至少有一位数字是5的所有数字,比如165、555、525。

4) 编写一个方法,返回某个字符在某个字符串中出现的次数。"abchahah"中'a'出现了3次。int GetCharCount(string s,char c)返回字符串s中c出现的次数。

 

如鹏网:http://www.rupeng.com

posted @ 2016-03-03 16:49  小居工作室  阅读(296)  评论(0编辑  收藏  举报