Home > Back-end >  The IP address of the dotted decimal into integer algorithm is A * 256 * 256 ^ 2 C ^ 3 B * 256 D, ne
The IP address of the dotted decimal into integer algorithm is A * 256 * 256 ^ 2 C ^ 3 B * 256 D, ne

Time:10-22

Want from other database reads the integer IP addresses, reduction in dotted decimal, can have a negative number, I do not know how to get the
The integrity can be achieved by A * 256 ^ 256 ^ 2 + 3 + B * C * 256 + D calculated
At first thought it was a 32-bit signed integer, can calculate the value of the wrong
Give advice or comments please!

CodePudding user response:

So calculate, then 128.0.0.0 and 0.0.0.0 calculate the decimal are 0; So is greater than the 128.0.0.0 IP are negative,
Such as 128.0.0.1 be 1
128.0.0.2 is - 2

CodePudding user response:

 unit Unit11; 

Interface

USES the
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

Type
TForm11=class (TForm)
Edt1: TEdit;
Btn1: TButton;
Edt2: TEdit;
Btn2: TButton;
Procedure btn1Click (Sender: TObject);
Procedure btn2Click (Sender: TObject);
Procedure FormCreate (Sender: TObject);
Private
The function ip2Int (const strIP: string) : Int64;
The function int2Ip (intIP: Int64) : string;
{Private declarations}
Public
{Public declarations}
end;

Var
Form11: TForm11;

Implementation

{$R *. DFM}

The function TForm11. Int2Ip (intIP: Int64) : string;
Var
N: int64;
The begin
Result:=';
N:=intIP SHR 24;
IntIP:=intIP xor (n SHL 24);
Result:=IntToStr (n) + '. '.

N:=intIP SHR 16;
IntIP:=intIP xor (n SHL 16);
Result: the Result=+ IntToStr (n) + '. '.

N:=intIP SHR 8;
IntIP:=intIP xor (n SHL 8);
Result: the Result=+ IntToStr (n) + '. '.

N:=intIP;
Result: the Result=+ IntToStr (n);
end;

The function TForm11. Ip2Int (const strIP: string) : Int64;
Var
LST: TStringList;
I: integer;
The begin
Result:=0;
LST:=TStringList. Create;
Try
LST. Delimiter:=';
LST. DelimitedText:=strIP;

For I:=0 to LST. Do the Count - 1
Result: the result=+ StrToInt64 (LST) [I] SHL (24 - I * 8);
The finally
LST. Free;
end;
end;

Procedure TForm11. Btn1Click (Sender: TObject);
The begin
Edt2. Text:=IntToStr (ip2Int (edt1. Text));
end;

Procedure TForm11. Btn2Click (Sender: TObject);
The begin
Edt1. Text:=int2Ip (StrToInt64 (edt2. Text));
end;

Procedure TForm11. FormCreate (Sender: TObject);
The begin
Edt1. Text:='192.168.1.1';
Btn1. Click;
end;

End.

CodePudding user response:

Have to unsigned 32-bit integer

CodePudding user response:

The same as the value of the database or not

CodePudding user response:

That doesn't call the decimal,

CodePudding user response:

reference 5 floor lhylhy reply:
that don't call the decimal,

You mean the IP address? You can also put it into hexadecimal conversion or binary

CodePudding user response:

refer to 6th floor Jikycc response:
reference 5 floor lhylhy reply: that doesn't call the decimal,
You mean the IP address? You can also put it into hexadecimal conversion or binary

This is the concept, lost concept will make a mistake,

CodePudding user response:

Turn int uint problem... If negative what's the problem..


I only care about what he is in the memory address values.


CodePudding user response:

IP is very simple, first is greater than 128 negative
Is the highest 32 bit integer is 1, the number after the complement is the absolute value of it,

CodePudding user response:

You should pay attention to other bigendian, so is the fourth byte,
  • Related