Title: How to convert a long IP address to a short address / port number
Question: How do I convert a 6 place IP address into a four place IP address and a port number?
Answer:
Some older internet protocols ( such as FTP ) send IP address's and port numbers in a 6 place format such as XXX.XXX.XXX.XXX.XX.XXX To Convert this into the usable 4 section IP address and port number, use the following code.
Procedure LongIPToShort(aLongIPAddress : String; Out ShortIPAddress : String; out PortNumber : Integer);
var I, DotPos, tempPort : Integer;
var tempAddy, temp : String;
var TempStr : String;
begin
tempAddy := '';
tempStr := '';
//Determine if the sender of the long IP is using , or . as the
// Delimator
if (POS(',', aLongIPAddress) 0) then TempStr := ','
else TempStr := '.';
For I := 1 to 4 do
begin
DotPOS := POS(TempStr, aLongIPAddress);
tempAddy := tempAddy + (Copy(aLongIPAddress, 1, (DotPos - 1)));
If I 4 then TempADdy := TempAddy + '.';
Delete(aLongIpAddress, 1, DotPos);
end;
DotPos := Pos(TempStr, aLongIpAddress);
temp := Copy(aLongIpAddress, 1, (DotPos - 1));
tempPort := (StrToInt(temp) * 256);
Delete(aLongIpAddress, 1, DotPos);
TempPort := tempPort + StrToInt(ALongIpAddress);
ShortIPAddress := TempADdy;
PortNumber := tempPort;
end;