c# - How to convert string to ascii values

Convert String to ASCII values
The String represents text as a sequence of UTF-16 code units. The String is a sequential collection of characters that is used to represent text. The String is a sequential collection of System.Char objects.

The following .net c# tutorial code demonstrates how we can convert a String instance to ASCII values. Here we will use ASCII encoding and the Encoding class GetBytes() method.

The Encoding class represents the character encoding. Encoding is the process of transforming a set of Unicode characters into a sequence of bytes and decoding is the process of transforming a sequence of encoded bytes into a set of Unicode characters. The Encoding ASCII property gets an encoding for the ASCII (7-bit) character set.

The Encoding GetBytes() method when overridden in a derived class, encodes a set of characters into a sequence of bytes. The Encoding GetBytes(String) method overload when overridden in a derived class, encodes all the characters in the specified string into a sequence of bytes.

The GetBytes(string s) method’s s parameter value is the String containing the characters to encode. This method returns a byte array containing the results of encoding the specified set of characters. So using this Encoding GetBytes(String) method we can get the ASCII values from a String instance.

The Encoding GetBytes(string s) method throws ArgumentNullException if the provided s is null. It also throws EncoderFallbackException if a fallback occurred and EncoderFallback is set to EncoderExceptionFallback.
string-to-ascii.aspx

<%@ Page Language="C#" AutoEventWireup="true"%>
  
<!DOCTYPE html>    
<script runat="server">  
    protected void Button1_Click(object sender, System.EventArgs e)  
    {  
        //this section create a string variable.  
        string birds = "Smew. Penguin.";

        Label1.Text = "string of birds..................<br />";
        Label1.Text += birds;

        //this line convert string to byte array.
        byte[] bytes = Encoding.ASCII.GetBytes(birds);

        Label1.Text += "<br /><br />ASSCII codes of string characters........";
        foreach(byte b in bytes)
        {
            //represent the ASCII code of characters.
            Label1.Text += "<br />" + b;
        }
    }    
</script>    
    
<html xmlns="http://www.w3.org/1999/xhtml">    
<head id="Head1" runat="server">    
    <title>c# example - string to ascii</title>    
</head>    
<body>    
    <form id="form1" runat="server">    
    <div>    
        <h2 style="color:MidnightBlue; font-style:italic;">    
            c# example - string to ascii
        </h2>    
        <hr width="550" align="left" color="Gainsboro" />    
        <asp:Label     
            ID="Label1"     
            runat="server"    
            Font-Size="Large"  
            >    
        </asp:Label>    
        <br /><br />  
        <asp:Button     
            ID="Button1"     
            runat="server"     
            Text="string to ascii"    
            OnClick="Button1_Click"  
            Height="40"    
            Font-Bold="true"    
            />    
    </div>    
    </form>    
</body>    
</html>
More c# examples