1) transform in binary value = 0 and 1 values 2 ) transform the "mask" in binary .... / 27 = n BITs used as network prefix ....... = 8 + 8 + 8 + 3 = 27 .... xxx.xxx.xxx.30
3) to "NETWORK" do the "AND" calculating using "IP" binary values
4) to "BROADCAST" do the "OR" calculating using the "NETWORK" binary value
----------------------------------------------
The higher the degree, the greater the respect given to the humblest!RAD 11.3
// IP4 長整數轉成字串 function IntToIp4Str(iAddress: Integer): string; begin Result := Format( '%d.%d.%d.%d', [iAddress shr 24 and $ff, iAddress shr 16 and $ff, iAddress shr 8 and $ff, iAddress shr 0 and $ff]); end;
// IP4 4個Bytes 轉成整數 function ByteToIP4Int(const A, B, C, D: Byte): Integer; begin Result := (A shl 24) + (B shl 16) + (C shl 8) + D; end;
// IP4 字串轉成整數 function StrToIP4Int(const AIP4Str: string): Integer; var S: TStrings; begin S := TStringList.Create; try S.Delimiter := '.'; S.DelimitedText := AIP4Str;
// do preeliminary check. The IP4 Address string must consists of 4 parts. Less or more than that would be invalid values if S.Count<>4 then raise Exception.Create('Invalid IP4 Address string');
Result := ByteToIP4Int(StrToInt(S[0]), StrToInt(S[1]), StrToInt(S[2]), StrToInt(S[3])); finally S.Free; end; end;
// 依照IP, SubnetMask 計算出 IP4 Broadcast IP // 就是網段中末尾IP,作為廣播使用 function calBroadcastIP(sIP, sMask:String):String; var iIP, iMask : integer; iBroadCast : integer; begin iIP := StrToIP4Int(sIP); iMask := StrToIP4Int(sMask); iBroadCast := iIP or (not iMask); Result := IntToIp4Str(iBroadCast); end;
Why kill a fly with a nuclear missile?
----------------------------------------------
The higher the degree, the greater the respect given to the humblest!RAD 11.3