帮助需要将此代码转换为C#

时间:2010-05-25 03:46:40

标签: c# vb.net

我根本不熟悉VB.NET。我需要将此函数转换为C#。有人可以帮我一把吗?

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 & New 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

4 个答案:

答案 0 :(得分:1)

该方法使用一些没有C#等价物的VB特定函数。功能可以很容易地近似,但要按原样使用,只需添加对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;

    }

答案 1 :(得分:1)

C#解决方案在

之下
    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;
    } 

答案 2 :(得分:0)

这些工具不了解某些VB功能。 C#中没有Conversion.Hex或Strings.Asc,String.Mid。有什么帮助吗?

答案 3 :(得分:0)

谢谢大家。我之前有点说过垃圾。工具很好,是的,他们转换为你。但令人困惑的是正确的。这些工具使用Microsoft.VisualBasic来执行

hexString = hexString + Conversion.Hex(Strings.Asc(Strings.Mid(sectionId,i,1)));

哪种情况好。有没有人看到这个有什么问题?

由于