Help need to convert this code to C#
I 'm not familiar with VB.NET at all. I need to convert this function to C#. Can anyone please give me a hand?
Public Function GetAppGUID(ByVal sectionId As String) As String
Dim hexString As String = Nothing
Dim i As Integer
Dim guidlen As Integer
guidlen = 16
If sectionId.Length < guidlen Then
sectionId = sectionId & N开发者_高级运维ew String(" ".Chars(0), guidlen - sectionId.Length)
End If
For i = 1 To guidlen
hexString = hexString & Hex(Asc(Mid(sectionId, i, 1)))
Next
GetAppGUID = hexString
End Function
C# solution is below
private string GetAppGUID(string sectionId)
{
string hexString = null;
int i = 0;
int guidLength = 0;
guidLength = 16;
if (sectionId.Length < guidLength)
{
sectionId = sectionId + new string(" "[0], guidLength - sectionId.Length);
}
foreach (char c in sectionId)
{
int tmp = c;
hexString += String.Format("{0:x2}", (uint)System.Convert.ToUInt32(tmp.ToString()))
}
return hexString;
}
The method uses some VB specific functions that do not have C# equivalents. The functionality could easily be approximated but to use as is, simply add a reference to Microsoft.VisualBasic
.
public string GetAppGUID(string sectionId)
{
string hexString = null;
int i = 0;
int guidlen = 0;
guidlen = 16;
if (sectionId.Length < guidlen)
{
sectionId = sectionId + new string(' ', guidlen - sectionId.Length);
}
for (i = 1; i <= guidlen; i++)
{
hexString = hexString
+ Microsoft.VisualBasic.Conversion.Hex(Microsoft.VisualBasic.Strings.Asc(Microsoft.VisualBasic.Strings.Mid(sectionId, i, 1)));
}
return hexString;
}
These tools don't know some VB functions. There is no Conversion.Hex or Strings.Asc, String.Mid in C#. Any help?
Thanks everyone. I kind of talking rubbish before. The tools are good and yes they convert for you. But is confusing right. The tools use Microsoft.VisualBasic to do the
hexString = hexString + Conversion.Hex(Strings.Asc(Strings.Mid(sectionId, i, 1)));
Which is kind of OK. Does anyone see anything wrong with this?
Thanks
精彩评论