KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Converting IPv4 address to IPv6
PRODUCT: 4D | VERSION: 16 | PLATFORM: Mac & Win
Published On: August 23, 2017

IPv4 addresses can be recognized in IPv6 as shown below in hex:



The following utility can convert a IPv4 address to IPv6:

// ----------------------------------------------------------
// Method: CONVERT_IPV4_TO_IPV6_STR
// Description: Method will convert the IPv4 string to IPv6
//
// Parameters:
// $1 (TEXT) - IPV4 string
//
// Output:
// $0 (TEXT) - IPV6 string or error of not a valid IPv4 string
// ----------------------------------------------------------
C_TEXT($1;$ip_v4;$tempIP)
C_TEXT($0)
C_LONGINT($posCnt;$posCntDot)
C_BOOLEAN($loop)
ARRAY TEXT($arrIPStr;0)

$loop:=True
$posCnt:=1

If (Count parameters=1)
  $ip_v4:=$1
  Repeat
   $posCntDot:=Position(".";$ip_v4;$posCnt)
   
   If ($posCntDot=0)
    If ($posCnt=Length($ip_v4))
     $tempIP:=Substring($ip_v4;$posCnt;1)
    Else
     $tempIP:=Substring($ip_v4;$posCnt;Length($ip_v4)-$posCnt+1)
    End if
    $loop:=False
   Else
    If (($posCntDot-$posCnt)=1)
     $tempIP:=Substring($ip_v4;$posCnt;1)
    Else
   
    $tempIP:=Substring($ip_v4;$posCnt;$posCntDot-$posCnt)
    End if
   End if
   
   If ((Num($tempIP)>=0) & (Num($tempIP)<=255))
    APPEND TO ARRAY($arrIPStr;Substring(String(Num($tempIP);"&x");5;2))
   End if
    $posCnt:=$posCntDot+1
   
  Until ($loop=False)
   
  If (Size of array($arrIPStr)=4)
   $0:="0:0:0:0:0:FFFF:"+$arrIPStr{1}+$arrIPStr{2}+":"+$arrIPStr{3}+$arrIPStr{4}
  Else
   $0:="Not a valid IP adress string"
  End if
Else
   $0:="Not a valid IP adress string"
End if


Here is an example of converting an IPv4 to IPv6:

C_TEXT($ipV4;$ipV6)

$ipV4:="192.168.99.1"

$ipV6:=CONVERT_IPV4_TO_IPV6_STR ($ipV4) // 0:0:0:0:0:FFFF:C0A8:6301