强制转换要在两者类型兼容的情况下才对,但INTEGER类型在内存块中放的是实际值,而STRING类型在内存块中放的是指针,你看一下INTTOSTR的源码就知道其复杂
度,INTTOSTR是另开辟了一块内存来放转换后的STRING;
DELPHI源码:
UNIT SYSUNTILS;
procedure FmtStr(var Result: string; const Format: string;
const Args: array of const);
var
Len, BufLen: Integer;
Buffer: array[0..4097] of Char;//注意这句
begin
BufLen := SizeOf(Buffer);
if Length(Format) < (BufLen - (BufLen div 4)) then
Len := FormatBuf(Buffer, BufLen - 1, Pointer(Format)^, Length(Format), Args)
else
begin
BufLen := Length(Format);
Len := BufLen;
end;
if Len >= BufLen - 1 then
begin
while Len >= BufLen - 1 do
begin
Inc(BufLen, BufLen);
Result := ''; // prevent copying of existing data, for speed
SetLength(Result, BufLen);
Len := FormatBuf(Pointer(Result)^, BufLen - 1, Pointer(Format)^,
Length(Format), Args);
end;
SetLength(Result, Len);
end
else
SetString(Result, Buffer, Len);
end;