简体   繁体   中英

cannot convert from byte to string (VB.Net to C#)

I have a code written in VB.net and translated it to C# (through some site). The code is not complicated so most of the translation turned out fine with no problems what so ever. One problem, though, is really causing me a headache and while it is referenced in many websites (including this one), I still can't get it to run. I have a function in VB.Net decalred as:

Public Shared Function UTF8HexToByte(ByVal str As String) As Byte

and the translator altered it to:

public static byte UTF8HexToByte(string str)

Later on, in my C# code, I'm calling this function:

   for (int i = 0; i <= Key1.Length - 1; i++)
            {
                Key1[i] = 16 * UTF8HexToByte(TempKey1[2 * i]) + UTF8HexToByte(TempKey1[2 * i + 1]);

            }

but what is working in VB.net does not work here. I'm getting this error:

Error 7 The best overloaded method match for 'maker.Resources.makeC.UTF8HexToByte(string)' has some invalid arguments

Error 8 Argument 1: cannot convert from 'byte' to 'string'

Now, I guess the problem is that UTF8HexToByte is receiving a string and returning a byte, whereas I give it a byte TempKey1 and not a string. But it does work at the VB.Net code, so I'm confused... here's the code for VB.Net:

For i As Integer = 0 To Key1.Length - 1
        Key1(i) = 16 * UTF8HexToByte(TempKey1(2 * i)) + UTF8HexToByte(TempKey1(2 * i + 1))
    Next

TempKey is a byte[], Key1 is a byte[].

Any help will do. I'm really stuck on this one.

Method signature tells you that it is expecting string. 'maker.Resources.makeC.UTF8HexToByte(string)' has some invalid arguments
Change

Key1[i] = 16 * UTF8HexToByte(TempKey1[2 * i]) + UTF8HexToByte(TempKey1[2 * i + 1]);

to

Key1[i] = 16 * UTF8HexToByte(System.Text.Encoding.UTF8.GetString(new []{TempKey1[2 * i]})) + UTF8HexToByte(System.Text.Encoding.UTF8.GetString(new []{TempKey1[2 * i + 1]}));

I'm not great with VB.NET, but a quick bit of test code shows that it's quite happy with me passing a byte into a function that expects string . This is not something C# is going to be happy with at all, so you've got no choice but to write some code which does the conversion.

To get that and keep the semantics of your original code, you need to figure out what VB.NET does to convert from byte to string and duplicate it.

It is not at all improbable that this is just a call to the byte 's ToString() method, so

Key1[i] = 16 * UTF8HexToByte(TempKey1[2 * i].ToString()) + UTF8HexToByte(TempKey1[2 * i + 1].ToString());

One thing you will find in C# is that it's a lot stricter about types and type conversions, forcing you to think about how data changes from one format to another.

There is no implicit or automatic conversion between byte and string in C#. Your best bet is to get a string via string.Format :

string.Format("{0}", yourByte)

As @Mr Lister noted, string.Format will return a string with the numerical value of the byte. If you need to interpret it as an ASCII character, then just cast it to char :

(char)yourByte

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM