Question : 10 lines of C#  to VB.NET translation supervising

Hi,

I need an expert to supervise the translation of a few lines of code, from C# to VB.NET
I use VB.NET 2008 and I was needed an StringToHex and HexToString that support Unicode Characters, in VB.NET. I found the sample of those two functions in C#, and have adapted this code for VB.NET.

The adapted VB.NET code works almost identical, the only difference is that in the VB.NET version appears one more character, at the end of the result. This character is: Unicode Character 'REPLACEMENT CHARACTER' (U+FFFD).

Although, I have made code to eliminate this character, I would like to have clean functions of StringToHex and HexToString in VB.NET.

So, I'm interested to know where exactly I have made the inaccurate adaptation of C# code to VB.NET code, and how to fix the code, for having clean functions without mistaken the Unicode character.

The C# code I found is at this page:
http://www.eggheadcafe.com/sample-code/csharp.NET/5c5e050f-7509-4ed5-aa58-6fd01ff2b3c6/c-net.aspx

The VB.NET adaptation of the C# code is this:
-----------------------------------------------------------------------------------------------------------
    Public Shared Function ConvertStringToHex(ByVal input As String, ByVal encoding As System.Text.Encoding) As String
        Dim stringBytes() As Byte = encoding.GetBytes(input)
        Dim sbBytes As StringBuilder = New StringBuilder(stringBytes.Length * 2)

        Dim b As Byte
        For Each b In stringBytes
            sbBytes.AppendFormat("{0:X2}", b)
        Next

        Return sbBytes.ToString()
    End Function

    Public Shared Function ConvertHexToString(ByVal hexInput As String, ByVal encoding As System.Text.Encoding) As String
        Dim numberChars As Integer = hexInput.Length
        Dim bytes() As Byte = New Byte(numberChars / 2) {}
        Dim i As Integer
        For i = 0 To numberChars - 1 Step 2
            bytes(i / 2) = Convert.ToByte(hexInput.Substring(i, 2), 16)
        Next

        Dim ReturnValue = encoding.GetString(bytes)

        If (AscW(ReturnValue.Substring(ReturnValue.Length - 1, 1)) = 65533) Then
            ReturnValue = ReturnValue.Substring(0, ReturnValue.Length - 1)
        End If

        Return ReturnValue
    End Function
-----------------------------------------------------------------------------------------------------------

The documentation for this undesired Unicode Character is at this page:
http://www.fileformat.info/info/unicode/char/fffd/index.htm


Thank you very much in advance.

Answer : 10 lines of C#  to VB.NET translation supervising

There is not *really* an extra character. In ConvertHexToString, when you created the array "bytes", you said that the upper-most index of the array should be "numberChars / 2", which in this test comes out to 14. Note that this is, as I said, the upper-most index, NOT the number of slots in the array. This means that the call to encoding.GetString() is trying to convert an uninitialized slot of the array into a character. You need to reduce the number of slots in the array by replacing line 31 of your last post with:
1:
Dim bytes() As Byte = New Byte((numberChars / 2) - 1) {}
Random Solutions  
 
programming4us programming4us