IPv6 Compress ahk
Re: IPv6 Compress ahk
Example:
1050:5:600:300c:326b
Code: Select all
IPv6 := "1050:0000:0000:0000:0005:0600:300c:326b"
IPv6 := RegExReplace(StrReplace(IPv6, "0000:"), ":0+", ":")
MsgBox, 4096, IPv6, % Clipboard := IPv6
ExitApp
Re: IPv6 Compress ahk
Need to get the double colon in there:
Result:
1050::5:600:300c:326b
Perhaps @teadrinker will show us how it can be done in a single RegEx.
Code: Select all
IPv6 := RegExReplace(RegExReplace(StrReplace(IPv6, "0000"), ":{3,}", "::"), ":0+", ":")
1050::5:600:300c:326b
Perhaps @teadrinker will show us how it can be done in a single RegEx.
Re: IPv6 Compress ahk
Try this:
Edited: The purpose of my regex was to match the supplied example, and it does so correctly. However, based on considerations below, it turned out that the proposed example is not fully representative of the standard. There are other proposals that seem to solve the problem.
Code: Select all
IPv6 := "1050:0000:0000:0000:0005:0600:300c:326b"
MsgBox, % RegExReplace(IPv6, "(:0000)+|:0*", ":")
; 1050::5:600:300c:326b
Last edited by sofista on 09 Aug 2022, 08:25, edited 1 time in total.
-
- Posts: 4311
- Joined: 29 Mar 2015, 09:41
- Contact:
Re: IPv6 Compress ahk
I'm not sure:
Code: Select all
IPv6 := "1050:0000:0000:0000:0005:0000:300c:326b"
MsgBox, % RegExReplace(IPv6, "(:0000)+|:0*", ":")
-
- Posts: 4311
- Joined: 29 Mar 2015, 09:41
- Contact:
Re: IPv6 Compress ahk
In addition, IPv6 can look like this: ff06:0:0:0:0:0:0:c3, that is compressing like ff06::c3.
-
- Posts: 4311
- Joined: 29 Mar 2015, 09:41
- Contact:
Re: IPv6 Compress ahk
Perhaps this one is correct:
Code: Select all
RegExReplace(IPv6, "^.*?\K(:0+){2,}(?=:)|:0+(?=[^:])", ":")
Re: IPv6 Compress ahk
Yes, matches the Web site....
Re: IPv6 Compress ahk
AHK v2 alternative (can be translated in v1 too):
Code: Select all
#Requires AutoHotkey v2.0-beta
MsgBox CompressIpv6("1050:0000:0000:0000:0005:0600:300c:326b") ; -> 1050::5:600:300c:326b
MsgBox CompressIpv6("ff06:0:0:0:0:0:0:c3") ; -> ff06::c3
CompressIpv6(Addr)
{
static STATUS_SUCCESS := 0
static AF_INET6 := 23
IN6_ADDR := Buffer(16, 0)
if (DllCall("ntdll\RtlIpv6StringToAddressW", "str", Addr, "ptr*", 0, "ptr", IN6_ADDR) = STATUS_SUCCESS)
{
Size := VarSetStrCapacity(&AddrString, 94)
if (DllCall("ws2_32\InetNtopW", "int", AF_INET6, "ptr", IN6_ADDR, "str", AddrString, "uint", Size))
return AddrString
}
return false
}
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
-
- Posts: 4311
- Joined: 29 Mar 2015, 09:41
- Contact:
Re: IPv6 Compress ahk
Small adjustment:
Code: Select all
IPv6 := "0:0:0:0:0:1:2:3"
MsgBox, % RegExReplace(IPv6, "^.*?\K((^|:)0+){2,}(?=:)|:0+(?=[^:])", ":")
Re: IPv6 Compress ahk
when trying to compress 2b06:0000:0000:1f2b:d77f:0000:0000:89ce
i get 2b06:0000:0000:1f2b:d77f:0000:0000:89ce
i get 2b06:0000:0000:1f2b:d77f:0000:0000:89ce
-
- Posts: 4311
- Joined: 29 Mar 2015, 09:41
- Contact:
Re: IPv6 Compress ahk
You used an incorrect colon character. Use this one :
-
- Posts: 4311
- Joined: 29 Mar 2015, 09:41
- Contact:
Re: IPv6 Compress ahk
The rest of the characters you used are also incorrect. You must type the address from an English (US) keyboard.
However, it's better using winapi instead of RegEx in this case. I translated @jNizM's code to v1:
However, it's better using winapi instead of RegEx in this case. I translated @jNizM's code to v1:
Code: Select all
MsgBox % CompressIpv6("2b06:0000:0000:1f2b:d77f:0000:0000:89ce")
CompressIpv6(Ipv6)
{
static STATUS_SUCCESS := 0, AF_INET6 := 23
VarSetCapacity(IN6_ADDR, 16, 0)
hr := DllCall("ntdll\RtlIpv6StringToAddress", "Str", Ipv6, "PtrP", _, "Ptr", &IN6_ADDR, "UInt")
if (hr != STATUS_SUCCESS)
throw "RtlIpv6StringToAddress failed, result: " . Format("{:#x}", hr)
VarSetCapacity(string, 94)
DllCall("ws2_32\InetNtop", "Int", AF_INET6, "Ptr", &IN6_ADDR, "WStr", string, "UInt", 47)
Return string
}
Re: IPv6 Compress ahk
teadrinker wrote: ↑08 Aug 2022, 16:56The rest of the characters you used are also incorrect. You must type the address from an English (US) keyboard.
However, it's better using winapi instead of RegEx in this case. I translated @jNizM's code to v1:Code: Select all
MsgBox % CompressIpv6("2b06:0000:0000:1f2b:d77f:0000:0000:89ce") CompressIpv6(Ipv6) { static STATUS_SUCCESS := 0, AF_INET6 := 23 VarSetCapacity(IN6_ADDR, 16, 0) hr := DllCall("ntdll\RtlIpv6StringToAddress", "Str", Ipv6, "PtrP", _, "Ptr", &IN6_ADDR, "UInt") if (hr != STATUS_SUCCESS) throw "RtlIpv6StringToAddress failed, result: " . Format("{:#x}", hr) VarSetCapacity(string, 94) DllCall("ws2_32\InetNtop", "Int", AF_INET6, "Ptr", &IN6_ADDR, "WStr", string, "UInt", 47) Return string }
why does this throw error with 00eb:0000:0000:0000:d8c1:0946:0272:0879
-
- Posts: 4311
- Joined: 29 Mar 2015, 09:41
- Contact:
Re: IPv6 Compress ahk
The characters you use have a wrong encoding. This may happen if you are copying text from somewhere. If you type the address from the english keyboard there is no error:
Code: Select all
MsgBox % CompressIpv6("00eb:0000:0000:0000:d8c1:0946:0272:0879")
CompressIpv6(Ipv6)
{
static STATUS_SUCCESS := 0, AF_INET6 := 23
VarSetCapacity(IN6_ADDR, 16, 0)
hr := DllCall("ntdll\RtlIpv6StringToAddress", "Str", Ipv6, "PtrP", _, "Ptr", &IN6_ADDR, "UInt")
if (hr != STATUS_SUCCESS)
throw "RtlIpv6StringToAddress failed, result: " . Format("{:#x}", hr)
VarSetCapacity(string, 94)
DllCall("ws2_32\InetNtop", "Int", AF_INET6, "Ptr", &IN6_ADDR, "WStr", string, "UInt", 47)
Return string
}