Convert.ToByte Method in C#



The Convert.ToByte method is used to convert a specified value to an 8-bit unsigned integer.

Let’s say we have a char variable.

Char charVal = ‘a’;

Now, convert it to an 8-bit unsigned integer.

byte byteVal = Convert.ToByte(charVal);

Let us see another example now.

Example

 Live Demo

using System;
public class Demo {
   public static void Main() {
      char[] charVal = { 'p', 'q', 'r', 's' };
      foreach (char c in charVal) {
         byte byteVal = Convert.ToByte(c);
         Console.WriteLine("{0} is converted to = {1}", c, byteVal);
      }
   }
}

Output

p is converted to = 112
q is converted to = 113
r is converted to = 114
s is converted to = 115

Advertisements