gpt4 book ai didi

windows - Inno Setup 脚本中的基本 IP 验证

转载 作者:可可西里 更新时间:2023-11-01 09:46:32 27 4
gpt4 key购买 nike

当我收集用户的输入时,我如何检查它是否是 IP 地址?

最佳答案

IP 地址(假设您指的是 IPv4)实际上是一个整数,但它通常写成由 . 分隔的四个数字。这些数字中的每一个都代表整数的一个字节值,因此每个数字都应该是 0 到 255(含)之间的数字。

function CheckIP(Input: String): Cardinal;
var
IP: Cardinal;
i : Integer;
Part: Integer;
PartValue: Cardinal;
PartValid: Boolean;
begin
Part := 3;
PartValue := 0;
PartValid := False;
IP := 0;
{ When a '.' is encountered, the previous part is processed. Force processing }
{ the last part by adding a '.' to the input. }
Input := Input + '.';

for i := 1 to Length(Input) do
begin

{ Check next character }
if Input[i] = '.' then
begin

if PartValue <= 255 then
begin
if PartValid then
begin
{ A valid part is encountered. Put it in the result. }
IP := IP or (PartValue shl (Part * 8));
{ Stop processing if this is the last '.' we put in ourselves. }
if i = Length(Input) then
Break;
{ Else reset the temporary values. }
PartValid := False;
PartValue := 0;
Dec(Part);
end
else
RaiseException('Empty part');
end
else
RaiseException('Part not within 0..255');

end
else if ((Input[i] >= '0') and (Input[i] <= '9')) then
begin

{ A digit is found. Add it to the current part. }
PartValue := PartValue * 10 + Cardinal((Ord(Input[i]) - Ord('0')));
PartValid := True;

end
else
begin

{ Any other character raises an exception }
RaiseException('Invalid character');

end;

{ If part < 0, we processed too many dots. }
if Part < 0 then
RaiseException('Too many dots');

end;

{ Check if we found enough parts. }
if Part > 0 then
RaiseException('Address most consist of 4 numbers');

{ If Part is not valid after the loop, the input ended in a dot. }
if not PartValid then
RaiseException('IP cannot end in a dot');

{ Return the calculated IP address as a cardinal. }
Result := IP;
end;

关于windows - Inno Setup 脚本中的基本 IP 验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6435567/

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