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

发布于 2024-11-29 02:27:14 字数 1539 浏览 2 评论 0原文

我有几个函数需要转换为 Pascal 以包含在我的 inno setup 安装程序中,以验证安装时的序列号。

任何帮助将不胜感激,因为我已经大约 8 年没有写过任何 pascal 了。

这是 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);

    }

I have a couple of functions that I need to convert to Pascal to include in my inno setup installer, to validate a serial number on install.

Any assistance would be appreciated, as I haven't written any pascal in about 8 years.

Here is the C# code.

  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);

    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

没︽人懂的悲伤 2024-12-06 02:27:14

你可以看一下这个(未经测试,除了如果我注释掉未实现的 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 帮助文件中列出为受支持。

You can take a look at this (untested, other than it compiles if I comment out the line with GetAuthIDFromRegistry which is unimplemented - couldn't test because I don't have any sample input/output data to work with from your question). It may not be 100% correct, but it should at least get you started in the right direction.

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;

All of the functions not containing source here are listed as supported in the InnoSetup help file in either the "Pascal Scripting: Support Functions Reference".

眼藏柔 2024-12-06 02:27:14

您可以尝试使用 C# 到 Oxygene 转换器 来将其转换为 Oxygene,Oxygene 用于德尔福棱镜。

问题是此代码使用了 InnoSetup 中不可用的 .NET 类(如 StringBuilder)和转换器(Int32.Parse)。

You can try the C# to Oxygene converter that does this conversion to Oxygene, the Object Pascal used in Delphi Prism.

The problem is that this code makes use of .NET classes (like StringBuilder) and converters (Int32.Parse) that are not available in InnoSetup.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文