gpt4 book ai didi

delphi - StrToInt 有 ansi 版本吗?

转载 作者:行者123 更新时间:2023-12-03 15:07:33 25 4
gpt4 key购买 nike

StrToInt 似乎没有 Ansi 重载。这是正确的吗?或者也许我错过了一些东西。StrToInt 坚持将我的 ansistrings 转换为字符串。

最佳答案

你是对的。 StrToInt 没有 ANSI 版本。可以在 AnsiStrings 单元中找到 ANSI 版本的标准函数,但那里什么也没有。

要么编写自己的函数来完成这项工作,要么接受使用 StrToInt 所需的转换。

编写自己的函数并不难。它可能看起来像这样:

uses 
SysConst; // for SInvalidInteger
....
{$OVERFLOWCHECKS OFF}
{$RANGECHECKS OFF}
function AnsiStrToInt(const s: AnsiString): Integer;

procedure Error;
begin
raise EConvertError.CreateResFmt(@SInvalidInteger, [s]);
end;

var
Index, Len, Digit: Integer;
Negative: Boolean;
begin
Index := 1;
Result := 0;
Negative := False;
Len := Length(s);
while (Index <= Len) and (s[Index] = ' ') do
inc(Index);
if Index > Len then
Error;
case s[Index] of
'-','+':
begin
Negative := s[Index] = '-';
inc(Index);
if Index > Len then
Error;
end;
end;
while Index <= Len do
begin
Digit := ord(s[Index]) - ord('0');
if (Digit < 0) or (Digit > 9) then
Error;
Result := Result * 10 + Digit;
if Result < 0 then
Error;
inc(Index);
end;
if Negative then
Result := -Result;
end;

这是 StrToInt 中的精简版本。它不处理十六进制,并且对于错误更加严格。在使用这段代码之前,我想测试一下这是否真的是您的瓶颈。

非常有趣的是,这段代码基于 RTL 源代码,无法返回 low(Integer)。修复这个问题并不太难,但是会使代码变得更加复杂。

关于delphi - StrToInt 有 ansi 版本吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24244445/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com