Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

String To Hex 2

Status
Not open for further replies.

jamesp0tter

Programmer
Feb 20, 2003
119
PT
Hello,

I need to know how to convert a TEXT string into a Hex string of the chars in the text string.

Something like:

text := 'hello';
hex := '68656C6C6F';

Is there any pre-defined function, like StrToWhatever ? I searched nearly everywhere and only found functions to convert already existing hex strings.

But not the one i need: text string to hex string.

Anyone, pls?

TIA!

jamesp0tter,
jamespotter@netcabo.pt

p.s.: sorry for my (sometimes) bad english :p
 
Here is a TextToHex function and some sample code on how to use it.

The sample code assumes that your form has two TEdit components and a TButton component. If you click on the button you will get the hex version of the string in Edit1 displayed in Edit2.
Code:
function TextToHex ( const s: string ): string;
var
 x: integer;
begin
 for x := 1 to Length(s) do
  result := result + IntToHex(Integer(s[x]),2);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
 Edit2.Text := TextToHex ( Edit1.Text );
end;


Andrew
Hampshire, UK
 
thank you, that worked as expected :)

But now i have other problem, i found a function on Google that allowed me to convert decimal to text ( '65' to 'A' ), but oh-so-great-win-xp crashed before i had the chance to save the wonderfully-working code, and now i can't find it again ... :
it was a tinny code, something like result := FormatFloat(blabla);

anyone can help ?

tkx :)

jamesp0tter,
jamespotter@netcabo.pt

p.s.: sorry for my (sometimes) bad english :p
 
You could use Chr and Ord functions to go between ascii codes and characters. For example,
Code:
function ChrToDec(AChar: Char): Integer;
begin
  Result := Ord(AChar);
end;

function DecToChr(ADec: Integer): Char;
begin
  Result := Chr(ADec);
end;

Clive [infinity]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer."
Paul Ehrlich
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top