How to convert char array to bytes in C#

1 Answer

0 votes
using System;

class Program
{
    static void Main() {
        char[] char_array = { 'a', 'g', '\x6F' };
        
		foreach (char ch in char_array) {
		   try {
			  byte b = Convert.ToByte(ch);
			  Console.WriteLine("{0} converted to {1}", ch, b);
		   }
		   catch (OverflowException) {
			  Console.WriteLine("Unable to convert");
		   }
		}
    }
}




/*
run:

a converted to 97
g converted to 103
o converted to 111

*/

 



answered Jan 21, 2021 by avibootz

Related questions

1 answer 184 views
2 answers 292 views
2 answers 236 views
1 answer 101 views
101 views asked Aug 20, 2023 by avibootz
1 answer 130 views
130 views asked Aug 20, 2023 by avibootz
1 answer 175 views
...