簡體   English   中英

如何將此代碼從VB轉換為Delphi

[英]How to convert this code from VB to Delphi

Public Function GetChecksum(AsciText As String) As Integer
    Dim i As Integer

    For i = 1 To Len(AsciText)
        GetChecksum = GetChecksum Xor Asc(Mid$(AsciText, i, 1))
    Next i
End Function

我對Delphi上的“ Mid $”一無所知。

首先,該代碼不是VB.net,而是VB6或VBA。 現在, Mid$是用於提取子字符串的函數。 在Delphi中,它對應於Copy 但是,由於僅提取單個字符,因此應改用[]索引運算符。 總體來說,該功能看起來像這樣。

function GetChecksum(const Input: AnsiString): Integer;
var
  i: Integer;
begin
  Result := 0;
  for i := 1 to Length(Input) do 
    Result := Result xor ord(Input[i]);
end;

我相信在原始的VB6代碼中, Integer是16位值。 在Delphi中, Integer是32位值。 我不認為這很重要,因為僅使用了低8位。 但是,您可以將Delphi代碼的返回類型更改為8位無符號Byte類型。

還要注意,您必須對以下事實感到滿意:您的代碼依賴於以某種8位編碼方式編碼的文本。 如果要使用Unicode文本,該怎么辦?

Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged
    ListBox1.Items.Clear()
    Try
        Dim wc As New Net.WebClient
        wc.Encoding = System.Text.Encoding.Default
        Dim wb As New System.Net.WebClient
        Dim sr As String = wb.DownloadString("")
        Dim regex As New Regex("")
        Dim matches As MatchCollection = regex.Matches(sr)
        For Each element As Match In matches
            ListBox1.Items.Add(element.Value)
        Next
    Catch ex As Exception

    End Try
End Sub

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM