如何将一些C#代码转换为Pascal?

时间:2011-08-12 17:06:15

标签: c# pascal

我需要将一些函数转换为Pascal以包含在我的inno安装程序中,以便在安装时验证序列号。

任何帮助都会受到赞赏,因为我在大约8年内没有写过任何助手。

这是C#代码。

  public static long DecodeAuthID(String keyset, String toDecode)
    {
        StringBuilder retval = new StringBuilder();            
        for (int i = 0; i < toDecode.Length; i++)
        {
            char[] toDecodeCharArray = toDecode.ToCharArray();
            retval.Append(keyset.IndexOf(toDecodeCharArray[i]));
        }
        return Int32.Parse(retval.ToString());
    }

    public static string ReverseString(string stringToReverse)
    {
        char[] values = stringToReverse.ToCharArray();            
        Array.Reverse(values);
        return new string(values);
    }

    private static void GetLocationFromAuthenticationID()
    {
        // Get Authentication Key from the Registry
        string registryValue = GetAuthIDFromRegistry();

        // Decode the Authentication Key to get the location            
        string value1         = ReverseString(registryValue);
        string value2         = value1.Substring(0, 12);
        string keyset         = ReverseString(value2);
        string valuesReversed = value1.Substring(12, value1.Length - 12);
        string values         = ReverseString(valuesReversed);

        // Decode the AuthID                    
        string authID = DecodeAuthID(keyset, values).ToString();

        // Convert to Location ID
        int locationID = Int32.Parse(authID) - (Int32.Parse(authID) - 1);

    }

2 个答案:

答案 0 :(得分:3)

你可以看看这个(未经测试的,如果我注释掉GetAuthIDFromRegistry没有实现的那一行,那么除了编译之外 - 无法测试因为我没有从您的问题中使用的任何样本输入/输出数据)。它可能不是100%正确,但它至少应该让你开始正确的方向。

function DecodeAuthID(KeySet: string; toDecode: string): longint;
var
  idx, c: Integer;
  Temp: string;
begin
  Temp := '';
  for idx := 1 to Length(toDecode) do
  begin
    // Replaces keyset.IndexOf. Handles no match found in KeySet just in case.
    c := Pos(toDecode[idx], KeySet);
    if c > 0 then
      Temp := Temp + KeySet[c];
  end;
  // Handles no values set in result by returning 0
  Result := StrToIntDef(Temp, 0);
end;

function ReverseString(stringToReverse: string): string;
var
  i: Integer;
begin
  Result := '';
  for i := 1 to Length(stringToReverse) do
    Result := stringToReverse[i] + Result;
end;

procedure GetLocationFromAuthenticationID;
var
  registryValue: string;
  value1, value2, keyset: string;
  valuesReversed: string;
  values: string;
  authID: LongInt;
  locationID: Integer;
begin
  // GetAuthIDFromRegistry code not provided in question.
  // See InnoSetup Help File, Pascal Scripting: Support Functions Reference,
  //   subheading "Registry functions"
  registryValue := GetAuthIDFromRegistry;
  value1 := ReverseString(registryValue);

  // Delphi strings are 1 based, as opposed to the C# char array's 0 base
  value2 := Copy(value1, 1, 12);
  keyset := ReverseString(value2);

  valuesReversed := Copy(Value1, 13, Length(value1) - 12);
  values := ReverseString(valuesReversed);
  authID := DecodeAuthID(keyset, values);
  locationID := authID - (authID - 1);
end;

“Pascal脚本:支持函数参考”中的InnoSetup帮助文件中列出了所有不包含source的函数。

答案 1 :(得分:1)

您可以尝试执行此转换为Oxygene的C# to Oxygene converter,即Delphi Prism中使用的Object Pascal。

问题是这段代码使用了InnoSetup中没有的.NET类(如StringBuilder)和转换器(Int32.Parse)。