How to Convert ASCII Char to Byte in C#?

Given a char, the task is to convert this ASCII character into a Byte in C#.

Examples:

Input: chr = 'a'
Output: 97
  
Input: chr = 'H'
Output: 72

Method 1: Naive Approach

Step 1: Get the character.

Step 2: Convert the character using the Byte struct

byte b = (byte) chr;

Step 3: Return or perform the operation on the byte 

Below is the implementation of the above approach:



C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to convert 
// ascii char to byte.
    
using System;
  
public class GFG{
    
    static public void Main ()
    
        char ch = 'G'
    
        // Creating byte 
        byte byt; 
    
        // converting character into byte 
        byt = (byte)ch; 
    
        // printing character with byte value
        Console.WriteLine("Byte of char \'" + ch + "\' : " + byt);
  
    
}

chevron_right


Output:

Byte of char 'G' : 71

Method 2: Using ToByte() Method: This method is a Convert class method. It is used to converts other base data types to a byte data type.

Syntax:

byte byt = Convert.ToByte(char); 

Below is the implementation of the above approach:

C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to convert 
// ascii char to byte.
    
using System;
  
public class GFG{
    
    static public void Main ()
    
        char ch = 'G'
    
        // Creating byte 
        byte byt; 
    
        // converting character into byte 
        // using Convert.ToByte() method
        byt = Convert.ToByte(ch); 
    
        // printing character with byte value
        Console.WriteLine("Byte of char \'" +
                    ch + "\' : " + byt);
  
    
}

chevron_right


Output:

Byte of char 'G' : 71

Method 3: Using GetBytes()[0] Method: The Encoding.ASCII.GetBytes() method is used to accepts a string as a parameter and get the byte array. Thus GetBytes()[0] is used to get the byte after converting the character into string.

Syntax:

byte byt = Encoding.ASCII.GetBytes(string str)[0]; 

Step 1: Get the character.

Step 2: Convert the character into string using ToString() method.

Step 3: Convert the string into byte using the GetBytes()[0] Method and store the converted string to the byte.

Step 4: Return or perform the operation on the byte.

Below is the implementation of the above approach:

C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to convert 
// ascii char to byte.
    
using System;
using System.Text;
  
public class GFG{
    
    static public void Main ()
    
        char ch = 'G'
          
        // convert to string
        // using the ToString() method
        string str = ch.ToString();
          
        // Creating byte 
        byte byt; 
    
        // converting character into byte 
        // using GetBytes() method
        byt = Encoding.ASCII.GetBytes(str)[0]; 
    
        // printing character with byte value
        Console.WriteLine("Byte of char \'" +
                          ch + "\' : " + byt);
  
    
}

chevron_right


Output:

Byte of char 'G' : 71



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.