[Delphi (Object Pascal)] Delphi版IP地址与整型互转 →→→→→进入此内容的聊天室

来自 , 2020-06-04, 写在 Delphi (Object Pascal), 查看 102 次.
URL http://www.code666.cn/view/5735c3a7
  1. unit Unit11;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  7.   Dialogs, StdCtrls;
  8.  
  9. type
  10.   TForm11 = class(TForm)
  11.     edt1: TEdit;
  12.     btn1: TButton;
  13.     edt2: TEdit;
  14.     btn2: TButton;
  15.     procedure btn1Click(Sender: TObject);
  16.     procedure btn2Click(Sender: TObject);
  17.     procedure FormCreate(Sender: TObject);
  18.   private
  19.     function ip2Int(const strIP: string): Int64;
  20.     function int2Ip(intIP: Int64): string;
  21.     { Private declarations }
  22.   public
  23.     { Public declarations }
  24.   end;
  25.  
  26. var
  27.   Form11: TForm11;
  28.  
  29. implementation
  30.  
  31. {$R *.dfm}
  32.  
  33. function TForm11.int2Ip(intIP : Int64) : string;
  34. var
  35.   n : int64;
  36. begin
  37.   Result := '';
  38.   n := intIP shr 24;
  39.   intIP := intIP xor (n shl 24);
  40.   Result := IntToStr(n) + '.';
  41.  
  42.   n := intIP shr 16;
  43.   intIP := intIP xor (n shl 16);
  44.   Result := Result + IntToStr(n) + '.';
  45.  
  46.   n := intIP shr 8;
  47.   intIP := intIP xor (n shl 8);
  48.   Result := Result + IntToStr(n) + '.';
  49.  
  50.   n := intIP;
  51.   Result := Result + IntToStr(n);
  52. end;
  53.  
  54. function TForm11.ip2Int(const strIP : string): Int64;
  55. var
  56.   lst : TStringList;
  57.   i : integer;
  58. begin
  59.   result := 0;
  60.   lst := TStringList.Create;
  61.   try
  62.     lst.Delimiter := '.';
  63.     lst.DelimitedText := strIP;
  64.  
  65.     for i := 0 to lst.Count - 1 do
  66.       result := result + StrToInt64(lst[i]) shl (24 - i * 8);
  67.   finally
  68.     lst.Free;
  69.   end;
  70. end;
  71.  
  72. procedure TForm11.btn1Click(Sender: TObject);
  73. begin
  74.   edt2.Text := IntToStr(ip2Int(edt1.Text));
  75. end;
  76.  
  77. procedure TForm11.btn2Click(Sender: TObject);
  78. begin
  79.   edt1.Text := int2Ip(StrToInt64(edt2.Text));
  80. end;
  81.  
  82. procedure TForm11.FormCreate(Sender: TObject);
  83. begin
  84.   edt1.Text := '192.168.1.1';
  85.   btn1.Click;
  86. end;
  87.  
  88. end.
  89.  
  90. //delphi/6452

回复 "Delphi版IP地址与整型互转"

这儿你可以回复上面这条便签

captcha