Fórum Dúvida convertendo Função #470323
17/02/2014
0
--Código em delphi
function Encrypt( S: String ): String;
var
i, Len: Integer;
Ret: String;
begin
Len := Length( S );
Ret := Copy( S, 1, Len );
for i := 1 to Len do begin
Ret[i] := Chr( not Ord( Ret[i] ) );
end;
Result := Ret;
end;
--código em C# que estou testando, estudando
public static bool Encrypt(string S)
{
Int16 i, Len = 0;
string Ret = "";
Len = (Int16)S.Length;
Ret = string.Copy( S, 1, Len );
for(i = 1; i <= Len; i++)
{
Ret[i] = Chr( not Ord( Ret[i] ) );
};
if (S = Ret)
{
return true;
}
else
{
return false;
}
}
Itamar Souza
Curtir tópico
+ 0Posts
18/02/2014
Murilo Teixeira
Não conheço Delphi, em C# essa função provavelmente ficaria como algo do tipo:
public static string Encrypt(string value)
{
var size = value.Length;
var retorno = new char[size];
for (int i = 0; i < size; i++)
{
var binary = Convert.ToByte(value[i]);
var binaryString = Convert.ToString(binary, 2);
var convertedString = NotValue(binaryString).PadLeft(8, '0');
var byteValue = Convert.ToByte(convertedString, 2);
var intValue = Convert.ToInt32(byteValue);
retorno[i] = Convert.ToChar(intValue);
};
return new string(retorno);
}
public static string NotValue(string oldString)
{
var size = oldString.Length;
var retorno = new char[size];
for (var i = 0; i < size; i++)
{
retorno[i] = oldString[i] == '0' ? '1' : '0';
}
return new string(retorno);
}
Gostei + 0
18/02/2014
Itamar Souza
Agradeço a ajuda, a minha dúvida e o seguinte, no delphi quando eu adiciono uma palavra e chamo esta função, eu tenho uma resposta da seguinte forma.
Ex: ramati retorna ž’ž‹–
Só que até o momento sem uma solução no C#
Gostei + 0
19/02/2014
Itamar Souza
Eu tenho este código:
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(Encrypt("ramati"));
}
public string Encrypt(string S)
{
int i, Len = 0;
string Ret = "";
Len = S.Length; // quantidade de letras
Ret = S.Trim(); // nome digitado
for (i = 0; i < Len; i++)
{
Ret = Ret + Chr((byte)(~((int)(S[i]))));
};
return Ret;
}
//método chr
public char Chr(int codigo)
{
MessageBox.Show(codigo.ToString()); // Para testes
return (char)codigo;
}
Ele me retorna quase tudo igual ao Delphi, menos a ultima informação.
veja que ele traz: 141, 158, 146,158,139,150,ramati
agora no delphi : 141,158,146,158,139,150,
[img]http://arquivo.devmedia.com.br/forum/imagem/156581-20140219-013836.png[/img]
agradeço
Gostei + 0
19/02/2014
Murilo Teixeira
public string Encrypt(string s)
{
// Encoding: 1252 – West European Latin
Encoding enconding = Encoding.GetEncoding(1252);
int len = s.Length; // quantidade de letras
byte[] byteArray = new byte[len];
for (int i = 0; i < len; i++)
{
byteArray[i] = ((byte) ~((s[i])));
};
return enconding.GetString(byteArray);
}Nesse caso utilizei o CodePage 1252 que representa o ANSI code page do windows. Depois da uma pesquisada sobre os CodePages.
Espero ter ajudado.
Abraços
Gostei + 0
19/02/2014
Itamar Souza
Cara, você me ajudou de mais, agradeço muito o seu empenho em ajuda na solução, a sua ajuda foi fundamental. Espero que você possa continuar ajudando e de certa forma aprendendo também, pois nunca saberemos tudo.
Agradeço
Gostei + 0
Clique aqui para fazer login e interagir na Comunidade :)