StealthRT
StealthRT

Reputation: 10552

VB.net Hex to byte array translated to C#

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

Upvotes: 0

Views: 79

Answers (2)

Albert D. Kallal
Albert D. Kallal

Reputation: 49254

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.

Upvotes: 1

Xavier J
Xavier J

Reputation: 4728

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}

Upvotes: 2

Related Questions