function lw2ip(lw: LongWord): string;
begin
Result := IntToStr(lw shr 24 and 255) + '.' +
IntToStr(lw shr 16 and 255) + '.' +
IntToStr(lw shr 8 and 255) + '.' +
IntToStr(lw and 255);
end;
function ip2lw(ip: String): LongWord;
var
idx: ShortInt;
begin
Result := 0;
ip := ip + '.';
while ip <> '' do
begin
idx := Pos('.', ip);
Result := Result shl 8 + StrToInt(Copy(ip, 1, idx - 1));
ip := Copy(ip, idx + 1, Length(ip) - idx);
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
lw: LongWord;
ip: string;
begin
lw := 3232235778;
Edit1.Text := lw2ip(lw);
ip := '192.168.1.2';
Edit2.Text := IntToStr(ip2lw(ip));
end;
已经测试过了,好用,效率也应该没有大问题。
不过这两个函数中没考虑输入数据异常的情况。