Hey all I am getting the following error when running this code:
byte[] bytes = new[] {
Convert.ToByte("&H" + Conversion.Hex(127)),
Convert.ToByte("&H" + Conversion.Hex(7)),
Convert.ToByte("&H" + Conversion.Hex(170)),
Convert.ToByte("&H" + Conversion.Hex(218)),
Convert.ToByte("&H" + Conversion.Hex(228)),
Convert.ToByte("&H" + Conversion.Hex(50)),
Convert.ToByte("&H" + Conversion.Hex(1)),
Convert.ToByte("&H" + Conversion.Hex(155)),
Convert.ToByte("&H" + Conversion.Hex(171)),
Convert.ToByte("&H" + Conversion.Hex(232)),
Convert.ToByte("&H" + Conversion.Hex(127))
};
The error is:
Input string was not in a correct format.
Originally the code above is from a VB.net to C# translation. The original Vb.net code looked like this:
Dim bytes() As Byte = {"&H" & Hex(127), "&H" & Hex(7), "&H" & Hex(170),
"&H" & Hex(218), "&H" & Hex(228), "&H" & Hex(50),
"&H" & Hex(1), "&H" & Hex(155), "&H" & Hex(171),
"&H" & Hex(232), "&H" & Hex(127)}
What do I need to do in order to get this working in C#?
Visual Basic performs an implicit conversion from string to hex, but C# cannot do that. Rather than doing all this extra fluff, why not just use:
byte[] bytes = new byte[] { 127, 7, 170, 218, 228, 50, 1, 155, 171, 232, 127};
When I switched from VB to C# years ago, I learned that C# is MUCH more streamlined!!! Truthfully though, the original VB code could have just as well omitted all the calls to the Hex
function.
Dim bytes As Byte() = New Byte() {127, 7, 170, 218, 228, 50, 1, 155, 171, 232, 127}