0

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#?

11
  • 4
    "C# uses 0x and VB.NET uses &H as the prefix to specify hexadecimal numbers" stackoverflow.com/a/10240636/1043380
    – gunr2171
    Commented Jan 6 at 19:39
  • 6
    I'm curious as to why you have to use the Conversion.Hex -> string concatenation -> Convert.ToByte method when the literals are right there...? Commented Jan 6 at 19:42
  • @ipodtouch0218 You talking like this? Convert.ToByte("0x127"), Convert.ToByte("0x7"), etc?
    – StealthRT
    Commented Jan 6 at 20:13
  • Even better, byte[] bytes = new[] { 127, ... }, like in @Xavier J's answer. Note that 0x127 is not the same as decimal 127 (0x7F). Commented Jan 6 at 20:14
  • 5
    That original VB code is the absolutely most horrible way you could have written that in VB. Commented Jan 6 at 20:16

2 Answers 2

2

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}
1
  • that did it. Thanks for the incite xavier!
    – StealthRT
    Commented Jan 7 at 5:29
1

Ok, so let's explain what is occurring here.

The VB example is not really correct, since you taking a "string" value of "&H", and then say using the Hex function (which ALSO is a string!).

So, say for 255, then the resulting string (and I STRESS the word a "string") is thus this:

So, for this expression "&H" & hex(255)

The we get a STRING - again, I stress the word "string".

Hence, you get this

 "&HFF"

Then due to automatic casting in vb, then that string is then converted into a byte value (0-255)

What this REALLY means?

Well, the VB code did not have to convert the value to a "string", and did not need to convert a byte value into a "hex string" value!

In other words, multiple conversions were occurring here, without such a need.

The developer should have written this in VB:

    Dim mybytes() As Byte =
        {127, 7, 170, 218, 228, 50, 1, 155, 171, 232, 127}

The values are "already" a integer of one byte length, and hence no need for converting to hex string, and then adding &H in front. Then of course due to VB auto casting, was converting the string back into a byte value!

The decimal numbers (0-255) are already in a valid byte format, so casing into a hex (string) , and then prefixing with &H (again a string) was simply not required.

So, in C#, then taking above, we have this:

        Byte[] mybytes = 
            { 127, 7, 170, 218, 228, 50, 1, 155, 171, 232, 127 };

And note that even inteli sense (VB or C#) will show if a number is not a valid byte value. Eg this:

enter image description here

In other words, only values 0-255 are allowed, and no real need to try and feed the array string values, and let VB auto cast that string back into a byte value.

1
  • Thank you for the detailed explanation Albert!
    – StealthRT
    Commented Jan 7 at 5:30

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.