c# - How to get bytes from a string

Get bytes from a String
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 get bytes from a String instance. Here we will use ASCII, Default, and UTF8 encoding and 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 Default property gets the default encoding for this .NET implementation and the Encoding UTF8 property gets an encoding for the UTF-8 format.

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 bytes from a String instance.
string-to-getbytes.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 plant = "Laurel";

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

        //convert string to byte array using various encoding.
        byte[] bytes = Encoding.ASCII.GetBytes(plant);
        byte[] bytesDefault = Encoding.Default.GetBytes(plant);
        byte[] bytesUTF8 = Encoding.UTF8.GetBytes(plant);

        Label1.Text += "<br /><br />bytes from string (encoding ASCII)........";
        foreach (byte b in bytes)
        {
            //represent the ASCII code of characters.
            Label1.Text += "<br />" + b;
        }

        Label1.Text += "<br /><br />bytes from string (encoding default)........";
        foreach (byte b in bytesDefault)
        {
            //default encoding 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 getbytes</title>    
</head>    
<body>    
    <form id="form1" runat="server">    
    <div>    
        <h2 style="color:MidnightBlue; font-style:italic;">    
            c# example - string to getbytes
        </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 getbytes"    
            OnClick="Button1_Click"  
            Height="40"    
            Font-Bold="true"    
            />    
    </div>    
    </form>    
</body>    
</html>
More c# examples