谁能帮助我将这段PHP小代码转换为Delphi? [英] Who can help me to convert this small PHP code to Delphi?

查看:37
本文介绍了谁能帮助我将这段PHP小代码转换为Delphi?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我在PHP中有一个小代码,想在Delphi中进行转换,但遇到了一些麻烦...
这是PHP代码:

I have a small code in PHP that I would like to convert in Delphi and I have some troubles...
Here is the PHP code :

$from = "121.198.0.0";
$to = "121.198.255.255";
$arry1 = explode(".", $from);
$arry2 = explode(".", $to);
$a1    = $arry1[0];
$b1    = $arry1[1];
$c1    = $arry1[2];
$d1    = $arry1[3];
$a2    = $arry2[0];
$b2    = $arry2[1];
$c2    = $arry2[2];
$d2    = $arry2[3];
while ($d2 >= $d1 || $c2 > $c1 || $b2 > $b1 || $a2 > $a1) {
    if ($d1 > 255) {
        $d1 = 1;
        $c1++;
    }
    if ($c1 > 255) {
        $c1 = 1;
        $b1++;
    }
    if ($b1 > 255) {
        $b1 = 1;
        $a1++;
    }
    echo "$a1.$b1.$c1.$d1<br>";
    $d1++;
}

在Delphi中,我从此开始:

And in Delphi I started with that :

procedure TForm1.Button1Click(Sender: TObject);
var
  Arry1, Arry2: TStringList;
  RangeFrom, RangeTo: string;
  a1, b1, c1, d1, a2, b2, c2, d2: string;
begin
  RangeFrom := Edit1.Text;
  RangeTo := Edit2.Text;
  Arry1 := Explode('.', RangeFrom);
  Arry2 := Explode('.', RangeTo);
  a1 := Arry1[0];
  b1 := Arry1[1];
  c1 := Arry1[2];
  d1 := Arry1[3];
  a2 := Arry1[0];
  b2 := Arry1[1];
  c2 := Arry1[2];
  d2 := Arry1[3];
  while (StrToInt(d2) >= StrToInt(d1))
    //and StrToInt(c2) > (StrToInt(c1)
    //and StrToInt(b2) > (StrToInt(b1)
    //and StrToInt(a2) > (StrToInt(a1)
  do
  begin
    if (StrToInt(d1) > 255) then
    begin
        d1 := '1';
        StrToInt(c1)+1;
    end;
    if (StrToInt(c1) > 255) then
    begin
        c1 := '1';
        StrToInt(b1)+1;
    end;
    if (StrToInt(b1) > 255) then
    begin
        b1 := '1';
        StrToInt(a1)+1;
    end;
    ListBox1.Items.Add(a1+'.'+b1+'.'+c1+'.'+d1);
    StrToInt(d1)+1;
  end;
end;

(对于爆炸功能,我使用:

(For the Explode function, I use : http://www.marcosdellantonio.net/2007/06/14/funcao-explode-do-php-em-delphi/)

有人可以帮助我在Delphi中使用此小代码吗?

Someone can help me to make working this small code in Delphi ?

先谢谢您了:)

贝尼

推荐答案

仅通过查看代码即可看到以下内容:

Here's what I can see just by looking at the code:

如果认为您以错误的顺序设置了 Explode 的参数.分隔符是您链接到的函数中的第二个参数.所以你应该写:

If think you have the parameters to Explode in the wrong order. The separator is the second parameter in the function to which you linked. So you should write:

Arry1 := Explode(RangeFrom, '.');
Arry2 := Explode(RangeTo, '.');


您正在通过不释放 Explode 函数返回的数组来泄漏内存.Delphi与PHP的不同之处在于,您通常必须管理创建的对象的内存.


You are leaking memory by not freeing the arrays returned by the Explode function. Delphi is different from PHP in that you often have to manage the memory of objects that you create.

a2 := Arry1[0];
b2 := Arry1[1];
c2 := Arry1[2];
d2 := Arry1[3];

这些应该是 Arry2 ,而不是 Arry1 .

您应该将字符串预先转换为整数.因此,像这样声明它们:

You should convert the strings to integers up front. So declare them like this:

a1, b1, c1, d1, a2, b2, c2, d2: Integer;

并编写这样的作业:

a1 := StrToInt(Arry1[0]);
b1 := StrToInt(Arry1[1]);
//etc. etc.


while ($d2 >= $d1 || $c2 > $c1 || $b2 > $b1 || $a2 > $a1)

翻译为:

while ((d2 >= d1) or (c2 > c1) or (b2 > b1) or (a2 > a1))


在while循环中,您需要这样的代码:


Inside the while loop you want code like this:

if d1 > 255 then
begin
  d1 := 1;
  inc(c1);
end;


使用 Format 函数,最容易将4个值转换回IP地址:


Converting the 4 values back to an IP address is easiest with the Format function:

ListBox1.Items.Add(Format('%d.%d.%d.%d', [a1, b1, c1, d1]));


您对 StrToInt 的功能有误解,它是一个将字符串作为输入接收并返回整数的函数.当您写时:


You have a mis-understanding of what StrToInt does it is a function that receives as input a string and returns an integer. When you write:

StrToInt(d1)+1

您没有修改只是输入变量的变量 d1 .当您进行我描述的其他更改时,该问题消失了,但我想指出来是为了您将来的利益.

you are not modifying the variable d1 which is just an input variable. That issue disappears when you make the other changes I describe, but I wanted to point it out for your future benefit.

您可能还想强制定期重新绘制列表框,以便您可以查看正在发生的情况.

You may also want to force a periodic repaint of the list box so that you can see what is happening.

将所有内容放在一起,您将拥有以下内容:

Put it all together and you have this:

var
  Arry1, Arry2: TStringList;
  RangeFrom, RangeTo: string;
  Count: Integer;
  a1, b1, c1, d1, a2, b2, c2, d2: Integer;
begin
  RangeFrom := Edit1.Text;
  RangeTo := Edit2.Text;
  Arry1 := Explode(RangeFrom, '.');
  try
    a1 := StrToInt(Arry1[0]);
    b1 := StrToInt(Arry1[1]);
    c1 := StrToInt(Arry1[2]);
    d1 := StrToInt(Arry1[3]);
  finally
    Arry1.Free;
  end;
  Arry2 := Explode(RangeTo, '.');
  try
    a2 := StrToInt(Arry2[0]);
    b2 := StrToInt(Arry2[1]);
    c2 := StrToInt(Arry2[2]);
    d2 := StrToInt(Arry2[3]);
  finally
    Arry2.Free;
  end;    
  Count := 0;
  while ((d2 >= d1) or (c2 > c1) or (b2 > b1) or (a2 > a1)) do
  begin
    if d1 > 255 then
    begin
      d1 := 1;
      inc(c1);
    end;
    if c1 > 255 then
    begin
      c1 := 1;
      inc(b1);
    end;
    if b1 > 255 then
    begin
      b1 := 1;
      inc(a1);
    end;
    ListBox1.Items.Add(Format('%d.%d.%d.%d', [a1, b1, c1, d1]));
    inc(Count);
    if Count mod 1000=0 then begin
      ListBox1.Repaint;
      ListBox1.TopIndex := ListBox1.Items.Count-1;
    end;

    inc(d1);
  end;
end;

这篇关于谁能帮助我将这段PHP小代码转换为Delphi?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆