using
System;
class
GFG {
public
static
void
Main()
{
try
{
byte
[] bytes = {32, 0, 0, 42, 0, 65, 0, 125, 0,
197, 0, 168, 3, 41, 4, 125};
Console.Write(
"Initial Array: "
);
PrintIndexAndValues(bytes);
Console.WriteLine(
"index element short value"
);
for
(
int
index = 1; index < bytes.Length;
index = index + 2) {
if
(index == bytes.Length - 1) {
Console.WriteLine();
Console.WriteLine(
"startIndex equals the"
+
" length of value minus 1."
);
short
values = BitConverter.ToInt16(bytes, index);
Console.WriteLine(
" {0} {1} {2}"
,
index, BitConverter.ToString(bytes, index, 2), values);
}
else
{
short
values = BitConverter.ToInt16(bytes, index);
Console.WriteLine(
" {0} {1} {2}"
,
index, BitConverter.ToString(bytes, index, 2),
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);
}
}
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();
}
}