# VBForums CodeBank > CodeBank - Other >  [DELPHI] - Convert String To Hex?

## Madboy

```
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;
```

----------


## Taqyon

Perhaps this is shorter:



```
{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:



```
{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.  :Duck:

----------


## davitz38

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

----------


## JJTrapani

Thanks Taqyon for the sample code.  Simple and elegant.

----------

