1. 程式人生 > >c#單純數字陣列轉換為ASCII碼

c#單純數字陣列轉換為ASCII碼

while語句顯示前十個英文字母

                   
static void Main(string[] args)
        {
            Console.WriteLine("英文字母前十個為:");
            int i = 65;
            while (i<76)
            {
                char c = (char)i;
                Console.WriteLine(c);
                i++;
            }
       Console.ReadKey();

        }
 

整形陣列ASCII轉換為字串

        static void Main(string[] args)
        {
            int code = 88;        //單個整型數值轉換為ASCII字元
            char c = (char)code;
            Console.WriteLine(c);
            Console.ReadKey();

            int[] arr = { 45, 87, 84, 85 };     //整型陣列轉換為ASCII字元
            foreach(int i in arr)
            {
                 char x=(char)i;
                 Console.WriteLine(x);
            }
            Console.ReadKey(); 
        }

Ascii碼轉換為字串

 static void Main(string[] args)
        {
            string str = "979899100";       
            string str1 = string.Empty;
            for (int i = 0; i < str.Length; i++)    
            {
                int j = int.Parse(str.Substring(i, 2));
                if (j < 64)
                {
                    j = int.Parse(str.Substring(i, 3));
                    i += 2;
                }
                else
                {
                    i += 1;
                }
                str1 += Encoding.ASCII.GetString(new byte[] { (byte)j });
            }
            Console.WriteLine(str1);
            Console.ReadKey();


單個字串轉換為數字字元

static void Main(string[] args)
        {
            string str = "b";                                        //定義一個字串
            byte[] array = System.Text.Encoding.ASCII.GetBytes(str); //將字串str根據ASCII碼轉換為byte型別
            int asc = (int)(array[0]);                               //將陣列元素強制轉換為int型
            //string AscIIstr1 = Convert.ToString(asc);              //將int再次迴轉為字串型
            Console.WriteLine("ascii中字元b為{0}", asc);
            Console.ReadKey();
        }

多個字串轉換為多個數字字串

        static void Main(string[] args)
        {
            string str2 = "Aaron";
            byte[] array = System.Text.Encoding.ASCII.GetBytes(str2);   //字串轉換成相應的ASCII陣列
            string AScii = null;                                         //定義一個空值字串    
            for(int i=0;i<array.Length;i++)                             //判斷陣列中元素的長度
            {
                int asciicode = (int)(array[i]);                        //將陣列arr中的元素轉換成整xing
                AScii += Convert.ToString(asciicode);                   //必須轉換為字元型才能輸出多個數值
            }
            Console.WriteLine(AScii);
            Console.ReadKey();
        }


以上是小白的個人總結,如有意識本週的地方希望大家給予寶貴的指導,小白感恩不盡
同時小白也會回訪哦!得意