Display ASCII Value of String in C#

Hi Programmers , Welcome to new article of c#.net. This article i’ll write the program to display ASCII Value of String in C# console application. To Achieve the exact output we need to use System.Text namespace. Include Encoding class
ASCII property and GetBytes method, pass string value inside getbytes method and store all into byte array arr. Traverse all values one by one using foreach loop. Let’s see the codes.

Directly Test Codes into Editor

using System;
using System.Text;

namespace ConsoleApp1 {
    class Program {       
        static void Main(string[] args) 
        {
            Console.Write("Enter Any String :");
            string str = Console.ReadLine();
            byte[] arr = Encoding.ASCII.GetBytes(str);

            Console.WriteLine("ASCII Value of String ");
            foreach(byte e in arr)
            {
                Console.WriteLine(" {0} = {1} ", e, (char)e);
            }
        }
    }
}

Happy Coding…Thanks.

Post Author: adama