Since a % get's encoded as %25 you should be able to pick them out of the string and change them back to their representative character.
To do this you'll need to find % in the str using Pos/PosEx and pull out the 2 digits after it (I think it's always 2)
This is off the top of my head, so apologies if it doesn't compile/parameters are in the wrong order etc. It should be enough to give you the general idea.
function GetNextHex(InStr:String;var Position:Integer):String;
var
NextHex: Integer;
begin
NextHex := PosEx('%', InStr, Position);
if (NextHex > -1) then
Result := Copy(InStr, NextHex, 3)
else
Result := '';
Position := NextHex;
end;
To change hex to chr, swap the % for a $ and use StrToInt
which you can then use with Char
or Chr
depending on your preference.
function PercentHexToInt(Hex: String):Integer;
var
str : string;
begin
if (Hex[1] <> '%') then Result := 0
else
begin
// Result := strtoint(StrToHex('$' + Copy(Hex, 1,2)));
str :=StringReplace(HEx,'%','',[rfReplaceAll,rfIgnoreCase]);
str:=trim(str);
Result := StrToInt(('$' +str));
end;
end;
With these you should be able to scan through the string replacing the hex values
function ReplaceHexValues(Str: String):String;
var
Position:Integer;
HexValue:String;
IntValue:Integer;
CharValue:String;
begin
Position := 0;
while(Position > -1)
begin
HexValue := GetNextHex(Str, Position);
IntValue := PercentHexToInt(HexValue);
CharValue := Char(IntValue);
if (CharValue = #0) then break;
//Note that Position Currently contains the the start of the hex value in the string
Delete(Str, Position, 3);
Insert(CharValue,Str,Position);
end;
Result:=Str;
end;