开发者

How to convert some C# code to Pascal?

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();
            开发者_如何学Goretval.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);

    }


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".


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜