# Thread: [DELPHI] - Convert String To Hex?

1. ## [DELPHI] - Convert String To Hex?

Code:
```function StringtoHex(Data: string): string;
var
i, i2: Integer;
s: string;
begin
i2 := 1;
for i := 1 to Length(Data) do
begin
Inc(i2);
if i2 = 2 then
begin
s  := s + ' ';
i2 := 1;
end;
s := s + IntToHex(Ord(Data[i]), 2);
end;
Result := s;
end;```

2. ## Re: [DELPHI] - Convert String To Hex?

Perhaps this is shorter:

Code:
```{Convert a string to a hex string, obviously twice as long. Rudementary encryption, good for passing funny characters as parameters in a url}
function StringToHex(S: String): String;
var I: Integer;
begin
Result:= '';
for I := 1 to length (S) do
Result:= Result+IntToHex(ord(S[i]),2);
end;```
The reverse:

Code:
```{Reverse of StringToHex(), assuming even size of 2 hex digits per represented character}
function HexToString(H: String): String;
var I: Integer;
begin
Result:= '';
for I := 1 to length (H) div 2 do
Result:= Result+Char(StrToInt('\$'+Copy(H,(I-1)*2+1,2)));
end;```
Might be of some use to someone.

3. ## Re: [DELPHI] - Convert String To Hex?

Hi guys, to double check my programs, I use this hex to string tool
pretty cool!
David

4. ## Re: [DELPHI] - Convert String To Hex?

Thanks Taqyon for the sample code. Simple and elegant.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

Featured

Click Here to Expand Forum to Full Width