1

I'm working with Delphi 7 application and BDE (legacy software). whenever the password is changed/saved from ODBC admin like this (example if the password was my,password )

enter image description here

It gets saved as my%2cpassword in the registry.

enter image description here

And my application reads the registry path

HKEY_CURRENT_USER\Software\ODBC\ODBC.INI\mysource
Password

then uses the password to execute a psql command,

   "C:\Program Files (x86)\PostgreSQL\9.0\bin\psql.exe"  -h localhost -p 5432 -d myDB -U myadmin -f "C:\Users\user\AppData\Roaming\ff.sql"

Since the password now has %2c inside it instead of , the authentication fails. As I read the password and write to the pgpass.conf file.

How to convert the Hex characters to proper string when the HEX character are mixed with normal character?

Erwin Brandstetter
  • 605,456
  • 145
  • 1,078
  • 1,228
psqluser
  • 103
  • 1
  • 2
  • 10

2 Answers2

3

This seems to be a straightforward urlencoding of the string.

%2c is , in urlencoding. Simple use url decode.

Check out this SO question for info Standard URL encode function?

Community
  • 1
  • 1
Toby Allen
  • 10,997
  • 11
  • 73
  • 124
2

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;
psqluser
  • 103
  • 1
  • 2
  • 10
James
  • 9,774
  • 5
  • 34
  • 58
  • ok , i got the first part as `StringReplace(Edit1.Text,'%25','%',[rfReplaceAll,rfIgnoreCase]);` .. can you tell me more on the second part? – psqluser Sep 26 '12 at 10:10
  • Thank u for the code, but how do i have to decrement `Position` in `ReplaceHexValues` ? as `Position:=Position-1;` as its going in infinite loop. I have done some modifications to code though – psqluser Sep 26 '12 at 11:03
  • PosEx should return -1 when the search value isn't found, so I was forward scanning the string until no more matches were found. The Position is passed into a var parameter so it gets updated in the GetNextHex method. Although I should have broken out of the loop at that point, I've updated the code to break when no more matches are found. – James Sep 26 '12 at 11:06
  • hi i have made changes to the code..but still it doesnt go out of the loop `hex` never becomes blank to break out – psqluser Sep 26 '12 at 11:17
  • Hey code works now.!!, sory cant +1 (rep too low) but accepted.(bow) – psqluser Sep 26 '12 at 11:20
  • Hi, where is this fucntion `StrToHex` ? which pas file? – psqluser Sep 26 '12 at 11:41
  • StrToInt Accepts Hex Strings. [see here](http://www.delphibasics.co.uk/RTL.asp?Name=StrToInt) – James Sep 26 '12 at 12:31