// C# program to demonstrate
// BitConverter.ToSingle(Byte[], Int32)
// Method
using System;
using System.Collections.Generic;
public class GFG {
// Main Method
public static void Main()
{
try {
// Define an array of byte values.
byte[] bytes = {0, 128, 63, 0, 0, 112, 65,
0, 255, 127, 71, 0, 0, 128,
59, 0, 0, 128, 47, 73, 70,
131, 5, 75, 6, 158, 63, 24};
// Display the values of the myArr.
Console.Write("Initial Array: ");
// calling the PrintIndexAndValues()
// method to print
PrintIndexAndValues(bytes);
// print char value
Console.WriteLine("index Array elements float values");
Console.WriteLine();
// getting float value and Display it
for (int index = 0; index < bytes.Length + 1;
index = index + 4) {
if (index == bytes.Length) {
Console.WriteLine();
Console.WriteLine("startIndex is greater than "+
"the length of bytes minus 1.");
float values = BitConverter.ToSingle(bytes, index);
Console.WriteLine(" {0} {1} {2}",
index, BitConverter.ToString(bytes,
index, 4), values);
}
else {
float values = BitConverter.ToSingle(bytes, index);
Console.WriteLine(" {0} {1} {2}",
index, BitConverter.ToString(bytes,
index, 4), 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();
}
}