2

这是我第一次为 Lockbox 安装库。我从 sourceforge 下载了 3.4.3 版并拥有 Delphi 7。第一步是让这个傻瓜在 Delphi 7 下编译,这简直就是地狱。我确实希望这些组件在安装后更易于使用。

好的。我有一个看起来像这样的单元。

unit uTPLb_StrUtils;

interface

uses
  SysUtils, uTPLb_D7Compatibility;

function AnsiBytesOf(const S: string): TBytes;

implementation

function AnsiBytesOf(const S: string): TBytes;
begin
//compiler chokes here
  **Result := TEncoding.ANSI.GetBytes(S);**
end;

end.

顺便说一句,兼容单元将 TBytes 定义为 TBytes = 字节压缩数组;

Delphi 7 扼杀了 TEncoding,因为它只存在于 D2009+ 中。我用什么代替这个功能?

4

3 回答 3

4

StringAnsiString在 Delphi 7 中是 8 位。只需将字符串分配TBytes给 ,并将字符串内容分配到其中:Length()Move()

function AnsiBytesOf(const S: AnsiString): TBytes;
begin
  SetLength(Result, Length(S) * SizeOf(AnsiChar));
  Move(PChar(S)^, PByte(Result)^, Length(Result));
end;

如果您想在政治上正确并符合实际TEncoding.GetBytes()情况,则必须将其转换String为 aWideString然后使用 Win32 APIWideCharToMultiBytes()函数将其转换为字节:

function AnsiBytesOf(const S: WideString): TBytes;
var
  Len: Integer;
begin
  Result := nil;
  if S = '' then Exit;
  Len := WideCharToMultiByte(0, 0, PWideChar(S), Length(S), nil, 0, nil, nil);
  if Len = 0 then RaiseLastOSError;
  SetLength(Result, Len+1);
  WideCharToMultiByte(0, 0, PWideChar(S), Length(S), PAnsiChar(PByte(Result)), Len, nil, nil);
  Result[Len] = $0;
end;
于 2014-09-23T16:57:40.987 回答
0
function Quux(const S: AnsiString): TBytes;
var
  Count: Integer;
begin
  Count := Length(S) * SizeOf(AnsiChar);
  {$IFOPT R+}
  if Count = 0 then Exit; // nothing to do
  {$ENDIF}
  SetLength(Result, Count);
  Move(S[1], Result[Low(Result)], Count);
end;
于 2014-09-23T16:58:05.123 回答
0

您可以在这里获得 LB 3.5:

http://lockbox.seanbdurkin.id.au/Grok+TurboPower+LockBox

改为尝试 3.5。

于 2014-09-24T12:02:31.907 回答