// C# program to demonstrate
// BitConverter.ToChar(Byte[], Int32);
// Method
using System;
class GFG {
// Main Method
public static void Main()
{
try {
// Define an array of byte values.
byte[] bytes = { 32, 0, 0, 42, 0, 65, 0, 125, 0,
197, 0, 168, 3, 41, 4, 125, 32 };
// Display the values of the myArr.
Console.Write("Initial Array: ");
// calling the PrintIndexAndValues()
// method to print
PrintIndexAndValues(bytes);
// print char value
for (int index = 1; index < bytes.Length - 1;
index = index + 2) {
char values = BitConverter.ToChar(bytes, index);
Console.WriteLine("index element char");
Console.WriteLine(" {0} {1} {2}",
index, bytes[index], values);
}
}
catch (ArgumentNullException e) {
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
catch (ArgumentOutOfRangeException e) {
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
catch (ArgumentException e) {
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
}
// Defining the method
// PrintIndexAndValues
public static void PrintIndexAndValues(byte[] myArr)
{
for (int i = 0; i < myArr.Length; i++) {
Console.Write("{0} ", myArr[i]);
}
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("initial Array in string: {0} ",
BitConverter.ToString(myArr));
Console.WriteLine();
}
}