Fórum Hexa para Ascii #192196
01/11/2003
0
Oi pessoal, consegui converter Ascii para Hexa com esta função.
{ Public declarations }
function AsciiToHex(Texto: String): string;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function TForm1.AsciiToHex(Texto: String): string;
var
Tamanho,i : integer;
PalavraHex : String;
begin
Tamanho := Length(Texto);
for i := 1 to Tamanho do begin
PalavraHex:= PalavraHex + IntToHex(Ord(Texto[i]),1)+ ´ ´; { coloquei + ´ ´ para deixar um espaço entre as palavras}
end;
Result := PalavraHex;
end;
procedure TForm1.Edit1Change(Sender: TObject);
begin
Edit2.Text := AsciiToHex(Edit1.Text);
end;
Assim, por exemplo, quando alguém digitar no edit1 (Clube Delphi), no edit2 aparecerá (43 6C 75 62 65 20 44 65 6C 70 68 69). Gostaria de saber como faço o contrário, isto é, Hexa para Ascii. Ou seja, quando alguém digitar (43 6C 75 62 65 20 44 65 6C 70 68 69) no edit2, no edit1 deve aparecer (Clube Delphi). Alguém pode ajudar?
[color=red:afe2799bad]Obrigado T+[/color:afe2799bad]
{ Public declarations }
function AsciiToHex(Texto: String): string;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function TForm1.AsciiToHex(Texto: String): string;
var
Tamanho,i : integer;
PalavraHex : String;
begin
Tamanho := Length(Texto);
for i := 1 to Tamanho do begin
PalavraHex:= PalavraHex + IntToHex(Ord(Texto[i]),1)+ ´ ´; { coloquei + ´ ´ para deixar um espaço entre as palavras}
end;
Result := PalavraHex;
end;
procedure TForm1.Edit1Change(Sender: TObject);
begin
Edit2.Text := AsciiToHex(Edit1.Text);
end;
Assim, por exemplo, quando alguém digitar no edit1 (Clube Delphi), no edit2 aparecerá (43 6C 75 62 65 20 44 65 6C 70 68 69). Gostaria de saber como faço o contrário, isto é, Hexa para Ascii. Ou seja, quando alguém digitar (43 6C 75 62 65 20 44 65 6C 70 68 69) no edit2, no edit1 deve aparecer (Clube Delphi). Alguém pode ajudar?
[color=red:afe2799bad]Obrigado T+[/color:afe2799bad]
Ginosam
Curtir tópico
+ 0
Responder
Posts
02/11/2003
Ginosam
Opa, uma correção: No título, eu quis dizer Hexa para String. Para não confundí-los, mudem o nome da função:
[color=red:a5a8b44f17]function AsciiToHex(Texto: String): string;[/color:a5a8b44f17] para
[color=green:a5a8b44f17]function MyStrToHex(Texto: String): string;[/color:a5a8b44f17]
Resumindo, este exemplo mostra um texto (string) convertido em Hexadecimal. Eu quero saber o contrário, como faço para converter Hexadecimal para String.
Também vou colocar a conversõa de String para o código Ascii
correspondente, mas deixarei isto para outra pergunta!
Valeu, pessoal!
[color=red:a5a8b44f17]function AsciiToHex(Texto: String): string;[/color:a5a8b44f17] para
[color=green:a5a8b44f17]function MyStrToHex(Texto: String): string;[/color:a5a8b44f17]
Resumindo, este exemplo mostra um texto (string) convertido em Hexadecimal. Eu quero saber o contrário, como faço para converter Hexadecimal para String.
Também vou colocar a conversõa de String para o código Ascii
correspondente, mas deixarei isto para outra pergunta!
Valeu, pessoal!
Responder
Gostei + 0
03/11/2003
Ginosam
[color=blue:e1936a138f]Oi, pessoal, quando eu disse que a conversão Ascii ia ficar para outra pergunta, não vai mais ficar, porque descobri sem querer![/color:e1936a138f]
A função é quase igual a MyStrToHex, muda só duas coisinhas. Vejam:
Aonde está IntToHex(Ord(Texto[i]),1) mude para:
IntToStr(Ord(Texto[i]))
Eu ainda pretendo colocar a representação para binário, se eu conseguir lhes aviso!
Bem, o código completo até agora fica assim:
**********************************************************
Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls,
Forms, Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Edit1: TEdit; {aqui você digita alguma coisa}
Edit2: TEdit; {aqui aparecerá a representação em Hexadecimal}
Edit3: TEdit; {aqui aparecerá a represemtação em Ascii}
procedure Edit1Change(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
function MyStrToHex(Texto: string): string;
function MyStrToAscii(Texto: string): string;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function TForm1.MyStrToAscii(Texto: string): string;
var
Tamanho,i : integer;
PalavraHex : String;
begin
Tamanho := Length(Texto);
for i := 1 to Tamanho do begin
PalavraHex:= PalavraHex + IntToStr(Ord(Texto[i]))+ ´ ´;
end;
Result := PalavraHex;
end;
function TForm1.MyStrToHex(Texto: String): string;
var
Tamanho,i : integer;
PalavraHex : String;
begin
Tamanho := Length(Texto);
for i := 1 to Tamanho do begin
PalavraHex:= PalavraHex + IntToHex(Ord(Texto[i]),1)+ ´ ´;
end;
Result := PalavraHex;
end;
procedure TForm1.Edit1Change(Sender: TObject);
begin
Edit2.Text:= MyStrToHex(Edit1.Text);
Edit3.Text:= MyStrToAscii(Edit1.Text);
end;
end.
A função é quase igual a MyStrToHex, muda só duas coisinhas. Vejam:
Aonde está IntToHex(Ord(Texto[i]),1) mude para:
IntToStr(Ord(Texto[i]))
Eu ainda pretendo colocar a representação para binário, se eu conseguir lhes aviso!
Bem, o código completo até agora fica assim:
**********************************************************
Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls,
Forms, Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Edit1: TEdit; {aqui você digita alguma coisa}
Edit2: TEdit; {aqui aparecerá a representação em Hexadecimal}
Edit3: TEdit; {aqui aparecerá a represemtação em Ascii}
procedure Edit1Change(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
function MyStrToHex(Texto: string): string;
function MyStrToAscii(Texto: string): string;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function TForm1.MyStrToAscii(Texto: string): string;
var
Tamanho,i : integer;
PalavraHex : String;
begin
Tamanho := Length(Texto);
for i := 1 to Tamanho do begin
PalavraHex:= PalavraHex + IntToStr(Ord(Texto[i]))+ ´ ´;
end;
Result := PalavraHex;
end;
function TForm1.MyStrToHex(Texto: String): string;
var
Tamanho,i : integer;
PalavraHex : String;
begin
Tamanho := Length(Texto);
for i := 1 to Tamanho do begin
PalavraHex:= PalavraHex + IntToHex(Ord(Texto[i]),1)+ ´ ´;
end;
Result := PalavraHex;
end;
procedure TForm1.Edit1Change(Sender: TObject);
begin
Edit2.Text:= MyStrToHex(Edit1.Text);
Edit3.Text:= MyStrToAscii(Edit1.Text);
end;
end.
Responder
Gostei + 0
Clique aqui para fazer login e interagir na Comunidade :)