c# - How to convert a string to a byte array

Convert a String to a byte Array
The Array class provides methods for creating, manipulating, searching, and sorting arrays. The Array class is not part of the System.Collections namespaces. However, it is still considered a collection because it is based on the IList interface. An element is a value in an Array. The length of an Array is the total number of elements it can contain. The Array has a fixed capacity.

The following .net c# tutorial code demonstrates how we can convert a String to a byte Array. That means we will get a byte Array instance from a String object. In this .net c# example code, 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 convert a String object to a byte Array.
convert-string-to-byte-array.aspx

<%@ Page Language="C#" AutoEventWireup="true"%>  
  
<!DOCTYPE html>  
<script runat="server">  
    protected void Button1_Click(object sender, System.EventArgs e)  
    {
        const string txt = "array examples";
        byte[] bytearray = Encoding.ASCII.GetBytes(txt);

        Label1.Text = "";
        foreach(byte b in bytearray)
        {
            Label1.Text += b + " = " + Convert.ToChar(b).ToString()+  "<br />";
        }
    }  
</script>  
  
<html xmlns="http://www.w3.org/1999/xhtml">  
<head id="Head1" runat="server">  
    <title>c# example - convert string to byte array</title>  
</head>  
<body>  
    <form id="form1" runat="server">  
    <div>  
        <h2 style="color:DarkBlue; font-style:italic;">  
            c# example - convert string to byte array
        </h2>  
        <hr width="550" align="left" color="LightBlue" />    
  
        <asp:Label   
            ID="Label1"   
            runat="server"  
            Font-Size="Large"  
            >  
        </asp:Label>  
        <br />
        <asp:Button   
            ID="Button1"   
            runat="server"   
            Text="convert string to byte array"  
            OnClick="Button1_Click"
            Height="40"  
            Font-Bold="true"  
            />  
    </div>  
    </form>  
</body>  
</html>
More c# examples