Microsoft
Software
Hardware
Network
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-co
de/csharp.
NET/5c5e05
0f-
7509-4e
d5-aa58-6f
d01ff2b3c6
/c-net.asp
x
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:X
2}", 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.Su
bstring(i,
2), 16)
Next
Dim ReturnValue = encoding.GetString(bytes)
If (AscW(ReturnValue.Substrin
g(ReturnVa
lue.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/unic
ode/char/f
ffd/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
Prevent Autonumber Increment
Win 7 Pro Virtual Pc XP Mode visible in the network but not accessible
ASP .NET IIS 7 Word Interop Mail Merge (Interact With Desktop)
Windows 7 roaming profiles
Running dcacls command - No Sid found error
SSIS/TSQL Three Part Naming for Temp Table
MS Access Table
Conditional selections in setup project
htaccess Partial Query String Rewrite
Customize Outlook Today by Adding Public Calendar