type TForm1 = class(TForm) Edit1: TEdit; Edit2: TEdit; Button1: TButton; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end;
var Form1: TForm1;
implementation
{$R *.DFM}
function IPStrToInt(s:String):Integer; begin result:=inet_addr(pchar(s)); end;
function IPIntToStr(I:Integer):String; var sinaddr:in_addr; begin sinaddr.S_addr:=i; result:=String(inet_ntoa(sinaddr)); end;
procedure TForm1.Button1Click(Sender: TObject); begin showmessage(inttostr(IPStrToInt('2.1.0.0'))); showmessage(IPIntToStr(258)); end;
end.
function PosRight(c:char;str:string):integer; var i:integer; begin for i:=length(str) downto 1 do begin if str[i]=c then begin result:=i; break; end; end; end;procedure IncIP(var IP:string); var s:string; i,j:integer; begin i:=PosRight('.',s); s:=copy(IP,i+1,length(IP)-i-1); j:=strtoint(s); inc(j); Delete(IP,i+1,length(IP)-i-1); IP:=IP+inttostr(j); end;
其实就用一个整数就行了 i:integer;IP :=IntToStr((i and $FF000000) shr 24)+'.'+ IntToStr((i and $FF0000) shr 16)+'.'+ IntToStr((i and $FF00) shr 8)+'.'+ IntToStr((i and $FF); inc(i); 当最后一位=$FF时,就是广播地址。
2)将长整数转成ip地址用inet_ntoawinsock 现成的 api:
inet_addr
inet_ntoa
注意intel的int,1.2.3.4转换成整数,前面的位数是低位,即 $04030201
用ntohl和ntohs转换主机、网络的long和short类型 unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls,winsock;
type
TForm1 = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
function IPStrToInt(s:String):Integer;
begin
result:=inet_addr(pchar(s));
end;
function IPIntToStr(I:Integer):String;
var
sinaddr:in_addr;
begin
sinaddr.S_addr:=i;
result:=String(inet_ntoa(sinaddr));
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
showmessage(inttostr(IPStrToInt('2.1.0.0')));
showmessage(IPIntToStr(258));
end;
end.
var
i:integer;
begin
for i:=length(str) downto 1 do
begin
if str[i]=c then
begin
result:=i;
break;
end;
end;
end;procedure IncIP(var IP:string);
var
s:string;
i,j:integer;
begin
i:=PosRight('.',s);
s:=copy(IP,i+1,length(IP)-i-1);
j:=strtoint(s);
inc(j);
Delete(IP,i+1,length(IP)-i-1);
IP:=IP+inttostr(j);
end;
i:integer;IP :=IntToStr((i and $FF000000) shr 24)+'.'+
IntToStr((i and $FF0000) shr 16)+'.'+
IntToStr((i and $FF00) shr 8)+'.'+
IntToStr((i and $FF);
inc(i);
当最后一位=$FF时,就是广播地址。
递增
Function IncStrIP(Const IP: String; Const Count: Integer = 1): String;
Var
IntIP: Integer;
StrIP: String;
Begin
IntIP := inet_addr(PChar(IP));
StrIP := IntToHex(IntIP, 8);
StrIP := Copy(StrIP, 7, 2) + Copy(StrIP, 5, 2) + Copy(StrIP, 3, 2) + Copy(StrIP, 1, 2); //==> C0 A8 00 01
StrIP := IntToHex(StrToInt(('$' + StrIP)) + Count, 8);
StrIP := IntToStr(StrToInt('$' + Copy(StrIP, 1, 2))) + '.' + IntToStr(StrToInt('$' + Copy(StrIP, 3, 2))) + '.' + IntToStr(StrToInt('$' + Copy(StrIP, 5, 2))) + '.' + IntToStr(StrToInt('$' + Copy(StrIP, 7, 2)));
Result := StrIP;
End;递减
Function DecStrIP(Const IP: String; Const Count: Integer = 1): String;
Var
IntIP: Integer;
StrIP: String;
Begin
IntIP := inet_addr(PChar(IP));
StrIP := IntToHex(IntIP, 8);
StrIP := Copy(StrIP, 7, 2) + Copy(StrIP, 5, 2) + Copy(StrIP, 3, 2) + Copy(StrIP, 1, 2); //==> C0 A8 00 01
StrIP := IntToHex(StrToInt(('$' + StrIP)) - Count, 8);
StrIP := IntToStr(StrToInt('$' + Copy(StrIP, 1, 2))) + '.' + IntToStr(StrToInt('$' + Copy(StrIP, 3, 2))) + '.' + IntToStr(StrToInt('$' + Copy(StrIP, 5, 2))) + '.' + IntToStr(StrToInt('$' + Copy(StrIP, 7, 2)));
Result := StrIP;
End;
2、Str1为“192.168.1.” Str2为1
3、Str2转换成strtoint(str2),Integer型,inc自动加一就可以了。
4、最后合并str1+str2就是最后要的结果了。