Jump to content

Convert IPv6 to IP numbers and backwards - (Moved)


Go to solution Solved by jchd,

Recommended Posts

Hello, I have been looking for information on how to pass an IPV6 to numbers and backwards. Can anybody help me?

Example
2001: 4860: 4860 :: 8888
to
42541956123769884636017138956568135816

and of

42541956123769884636017138956568135816
to
2001: 4860: 4860 :: 8888

Thank you
Link to comment
Share on other sites

  • Developers

Moved to the appropriate forum, as the Developer General Discussion forum very clearly states:

Quote

General development and scripting discussions.


Do not create AutoIt-related topics here, use the AutoIt General Help and Support or AutoIt Technical Discussion forums.

Moderation Team

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Welcome to AutoIt and the forum!

Stackoverflow has some examples (e.g. https://stackoverflow.com/questions/38310892/formula-to-convert-ipv6-address-to-ip-number/39011245).
You "simply" need to translate the code to AutoIt ;)

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Or let the converters on the Web do the job (e.g. https://www.ipaddressguide.com/ipv6-to-decimal)
Use the WinHTTP UDF (or any of the HTTP UDFs described in the wiki) to grab the result.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

  • Solution

Example code for IPv6 to uint128 code:

#include "..\include\BigNum.au3"

Local $sIPv6 = "2001: 4860: 4860 :: 8888"

; there shouldn't be spaces and add trailing column
$sIPv6 = StringReplace($sIPv6, ' ', '') & ':'

; split string on columns
Local $aIPv6 = StringRegExp($sIPv6, "([[:xdigit:]]{0,4}):", 3)

; expand double columns to strings of zeroes
; and store hex values in structure uword elements
Local $tIPv6_16 = DllStructCreate("ushort[8]")
Local $tIPv6_64 = DllStructCreate("uint64[2]", DllStructGetPtr($tIPv6_16))
For $i = 0 To 7
    If $aIPv6[$i] == "" Then
        $aIPv6[$i] = "0"
        For $j = 0 To 7 - UBound($aIPv6)
            _ArrayInsert($aIPv6, $i + 1 + $j, "0")
        Next
    EndIf
    DllStructSetData($tIPv6_16, 1, Number("0x" & $aIPv6[$i]), 8 - $i)
Next

; fetch hi and lo uint64 values
Local $iHi64 = _UintToString(DllStructGetData($tIPv6_64, 1, 2), 10)
Local $iLo64 = _UintToString(DllStructGetData($tIPv6_64, 1, 1), 10)
; shift left hi value by 64 bits
ConsoleWrite(_bignum_parse($iHi64 & " * 18446744073709551616 + " & $iLo64) & @LF)

Func _UintToString($i, $base)
    Return DllCall("msvcrt.dll", "wstr:cdecl", "_ui64tow", "uint64", $i, "wstr", "", "int", $base)[0]
    Return $aRes[0]
EndFunc   ;==>_UintToString

The reverse conversion left as exercise to readers.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

Timeout!

Since noone posted the reverse conversion, here's what it could look like:

#include "..\include\BigNum.au3"

Local $sIPv6 = "42541956123769884636017138956568135816"

; split into high and low 64-bit values
Local $sIPv6Hi = _BigNum_Round(_BigNum_Div($sIPv6, "18446744073709551616"), 0)
Local $sIPv6Lo = _BigNum_Mod($sIPv6, "18446744073709551616")

Local $tIPv6_16 = DllStructCreate("ushort[8]")
Local $tIPv6_64 = DllStructCreate("uint64[2]", DllStructGetPtr($tIPv6_16))

; fill structures
DllStructSetData($tIPv6_64, 1, _StringToUint($sIPv6Hi, 10), 2)
DllStructSetData($tIPv6_64, 1, _StringToUint($sIPv6Lo, 10), 1)

; grab 8 hex values
Local $sIPv6Hex
For $i = 8 To 1 Step -1
    $sIPv6Hex &= ":" & Hex(DllStructGetData($tIPv6_16, 1, $i), 4)
Next
$sIPv6Hex = StringTrimLeft($sIPv6Hex, 1)

; contract first string of 0x0000
$sIPv6Hex = StringRegExpReplace($sIPv6Hex, "0000(:0000)*", "", 1)
ConsoleWrite($sIPv6Hex & @LF)

Func _StringToUint($s, $base)
    Return DllCall("msvcrt.dll", "uint64:cdecl", "_wcstoui64", "wstr", $s, "ptr*", 0, "int", $base)[0]
EndFunc   ;==>_StringToUint

If the string of 0x0000 is leading or trailing, one needs to add a column to the result (not tested).

Edited by jchd

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...