Error while converting Delphi function to C#

Asked

Viewed 143 times

1

Good evening, everyone!

I’m trying to convert a Delphi function to c# and I’m getting it, where am I going wrong?

Delphi code:

function TCriptografa.CriptoBinToText(SText: string): string;
var
  SPos: Integer;
  BKey: Byte;
  S: string[1];
begin
  Result := '';
  {converte}
  for SPos := 1 to Length(SText) do
    begin
      S := Copy(FKey, (SPos mod Length(FKey)) + 1, 1);
      BKey := Ord(S[1]) + SPos;
      Result := Result + Chr(Ord(SText[SPos]) xor BKey);
    end;
  {inverte Result}
  Result := Invert(Result);
end;

function TCriptografa.HexToDec(Number: string): Byte;
begin
  Number := UpperCase(Number);
  Result := (Pos(Number[1], '0123456789ABCDEF') - 1) * 16;
  Result := Result + (Pos(Number[2], '0123456789ABCDEF') - 1);
end;


function TCriptografa.CriptoHexToText(SText: string): string;
var
  SPos: Integer;
begin
  Result := '';
  for SPos := 1 to (Length(SText) div 2) do
    Result := Result + Chr(HexToDec(Copy(SText, ((SPos * 2) - 1), 2)));
  {converte para texto}
  Result := CriptoBinToText(Result);
end;

Code C#

        public static string BinaryToString(string data)
        {
            List<Byte> byteList = new List<Byte>();

            for (int i = 0; i < data.Length; i += 8)
            {
                byteList.Add(Convert.ToByte(data.Substring(i, 8), 2));
            }
            return Encoding.ASCII.GetString(byteList.ToArray());
        }

        public static string FromHexString(string hexString)
        {
            var bytes = new byte[hexString.Length / 2];
            for (var i = 0; i < bytes.Length; i++)
            {
                bytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
            }

            return Encoding.Unicode.GetString(bytes); 
        }


        public static string CriptoHexToText(string Texto)
        {
            string sResult = "";
            for (int i = 0; i <= (Texto.Length-1)/2; i++)
            {
                sResult += Texto.Substring(i, 2);
                sResult += FromHexString(sResult);
            }
            sResult = BinaryToString(sResult);

            return sResult;
        }

Is returning error: No recognizable digit found.

  • puts an example of the data string you are using

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.