KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Convert IPv6 address to IPv4
PRODUCT: 4D | VERSION: 16 | PLATFORM: Mac & Win
Published On: February 1, 2018

Here is a utlity to convert a IPv6 address to IPv4 with the following methods below.

This first method will be used in the overall method which converts the Hex value string to long string:

// ----------------------------------------------------------
// Method: CONVERT_HEX_TO_LONG_STR
// Description: Method will convert the Hex string to long
// string.
//
// Parameters:
// $1 (TEXT) - Hex string
//
// Output:
// $0 (TEXT) - Long string
// ----------------------------------------------------------
C_TEXT($1;$hexStr)
C_TEXT($0)
C_LONGINT($index;$long;$strLen)

$hexStr:=$1
$long:=0

For ($index;1;Length($hexStr))
  $long:=($long << 4)+Position($hexStr[[$index]];"123456789ABCDEF";1;$strLen)
End for

$0:=String($long)


The utility method will convert the IPv6 address in string to IPv4 to string:

// ----------------------------------------------------------
// Method: CONVERT_IPV6_TO_IPV4_STR
// Description: Method will convert the IPv6 string to IPv4
//
// Parameters:
// $1 (TEXT) - IPV6 string
//
// Output:
// $0 (TEXT) - IPV4 string
// ----------------------------------------------------------
C_TEXT($1;$ipv6;$firstSet;$secondSet)
C_TEXT($0;$firstOct;$secondOct;$thirdOct;$fourthOct)
C_TEXT($firstSetSub1;$firstSetSub2)
C_TEXT($secondSetSub1;$secondSetSub2)
C_LONGINT($posColon;$posColonCnt;$posCnt)
C_BOOLEAN($loop;$ipv4Valid)

$ipv6:=$1
$loop:=True
$posColonCnt:=0
$posCnt:=1
$ipv4Valid:=true

Repeat
  $posColon:=Position(":";$ipv6;$posCnt)
  If ($posColon>0)
    $posColonCnt:=$posColonCnt+1
    $posCnt:=$posColon+1
  End if
   
  If ($posColonCnt>=6)
    $loop:=False
    If (lowercase(substring($ipv6;1;14))="0:0:0:0:0:ffff") // check if a valid IPv4 address
   
     $posColon:=Position(":";$ipv6;$posCnt)
     $firstSet:=Substring($ipv6;$posCnt;$posColon-$posCnt)
     $secondSet:=Substring($ipv6;$posColon+1;Length($ipv6)-$posColon+1)
    
     If ((Length($firstSet)=1) | (Length($firstSet)=2)) //Second Octet
      $firstOct:="0"
      $secondOct:=CONVERT_HEX_TO_LONG_STR ($firstSet)
     Else // First and Second Octet
      If (Length($firstSet)=3)
       $firstSetSub1:=Substring($firstSet;1;1)
       $firstSetSub2:=Substring($firstSet;2;2)
      Else
       $firstSetSub1:=Substring($firstSet;1;2)
       $firstSetSub2:=Substring($firstSet;3;2)
      End if
      $firstOct:=CONVERT_HEX_TO_LONG_STR ($firstSetSub1)
      $secondOct:=CONVERT_HEX_TO_LONG_STR ($firstSetSub2)
     End if
   
     If ((Length($SecondSet)=1) | (Length($SecondSet)=2)) //Second Octet
      $thirdOct:="0"
      $fourthOct:=CONVERT_HEX_TO_LONG_STR ($SecondSet)
     Else // First and Second Octet
      If (Length($firstSet)=3)
       $secondSetSub1:=Substring($secondSet;1;1)
       $secondSetSub2:=Substring($secondSet;2;2)
      Else
       $secondSetSub1:=Substring($secondSet;1;2)
       $secondSetSub2:=Substring($secondSet;3;2)
      End if
      $thirdOct:=CONVERT_HEX_TO_LONG_STR ($SecondSetSub1)
      $fourthOct:=CONVERT_HEX_TO_LONG_STR ($SecondSetSub2)
     End if
   Else
    $ipv4Valid:=False
   End if
  End If
   
Until($loop=False)

If ($ipv4Valid=True)
  $0:=$firstOct+":"+$secondOct+":"+$thirdOct+":"+$fourthOct
Else
  $0:="Not an IPv4 type of address"
End If


Note: If the IPv6 does not start with "0:0:0:0:0:ffff:" it is not an IPv4 address.


Here is an example of using the method:

C_TEXT($ipv6;$ipv4)

$ipv6:="0:0:0:0:0:ffff:7f00:1"

$ipv4:=CONVERT_IPV6_TO_IPV4_STR ($ipv6) // 127.0.0.1