1

我正在使用 Delphi 7 应用程序和 BDE(遗留软件)。每当像这样从 ODBC 管理员更改/保存密码时(例如,如果密码是 my,password

在此处输入图像描述

它被保存my%2cpassword在注册表中。

在此处输入图像描述

我的应用程序读取注册表路径

HKEY_CURRENT_USER\Software\ODBC\ODBC.INI\mysource
密码

然后使用密码执行 psql 命令,

   "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"

由于密码现在%2c在其中而不是,身份验证失败。当我读取密码并写入pgpass.conf文件时。

十六进制字符与普通字符混合时如何将十六进制字符转换为正确的字符串?

4

2 回答 2

3

这似乎是字符串的直接 urlencoding。

%2c 是,在 urlencoding 中。简单使用 url 解码。

查看此 SO 问题以了解信息标准 URL 编码功能?

于 2012-09-26T18:20:20.657 回答
2

由于% get 被编码为%25您应该能够从字符串中挑选出它们并将它们改回它们的代表字符。

为此,您需要使用 Pos/PosEx 在 str 中找到%并拉出后面的 2 位数字(我认为它总是 2)

这不是我的想法,所以如果它没有编译/参数顺序错误等,请道歉。这应该足以给你一个大致的想法。

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;

要将十六进制更改为 chr,请将%换成$并使用它,然后您可以根据自己的喜好StrToInt使用Char或使用它。Chr

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;

有了这些,您应该能够扫描替换十六进制值的字符串

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;
于 2012-09-26T09:55:19.407 回答