Tech Tip: Convert an IP Address stored as a Longint to an IP Address string
PRODUCT: 4D | VERSION: 11.5 | PLATFORM: Mac & Win
Published On: February 19, 2010
In a related Tech Tip, Convert an IP Address string to a Longint, the benefits and proceedures of storing a "dotted-decimal notation" IP address (IPv4) as a longint were covered.
Once you have the IP Adresses stored as longints you will need a way to retrieve that data as a string. This function performs the reverse operation of converting the IP as longint back into IP address in String, "dotted-decimal notation," format. It is also an excellent example of using hexidecimal notation and bit shifting.
If (True) If (False) `***************************************************************************** `// `// INET_LongIntToAddress `// `// Purpose: Retrieve an IP Address `// `// $0 - STRING - IP Address string `// $1 - LONGINT - IP Address as a Longint `// `***************************************************************************** End if C_TEXT($MethodName_T) $MethodName_T:=Current method name `===================== Declare Variables ================================== `method_parameters_declarations C_STRING(15;$0;$IPAddress_A15) C_LONGINT($AddressAsLong_L;$1) `-------------------------------------------------------------------------------- `method_wide_constants_declarations `-------------------------------------------------------------------------------- `local_variable_declarations End if `====================== Initialize and Setup ================================ $AddressAsLong_L:=$1 $IPAddress_A15:="" `======================== Method Actions ================================== $IPAddress_A15:=String($AddressAsLong_L&0x00FF) $IPAddress_A15:=String(($AddressAsLong_L >> 8) &0x00FF )+"."+$IPAddress_A15 $IPAddress_A15:=String(($AddressAsLong_L >> 16) &0x00FF )+"."+$IPAddress_A15 $IPAddress_A15:=String(($AddressAsLong_L >> 24) &0x00FF )+"."+$IPAddress_A15 `======================== Clean up and Exit ================================= $0:=$IPAddress_A15 |