Quantcast
Channel: Active questions tagged jq - Stack Overflow
Viewing all articles
Browse latest Browse all 522

JQ function to convert ipv6 address to num and num to addr [closed]

$
0
0

I have below python code, which works for converting ipv6 address to number and number to address

def ipv6_to_number(ipv6):    # Split the IPv6 address into segments    parts = ipv6.split(':')    # Expand the shorthand notation    expanded_parts = []    for part in parts:        if part == '':            # Handle the '::' shorthand by inserting zeros            missing_zeroes = 8 - len([p for p in parts if p])            expanded_parts.extend(['0'] * missing_zeroes)        else:            expanded_parts.append(part)    # Convert each segment to a 16-bit number and concatenate them    number = 0    for part in expanded_parts:        number = (number << 16) + int(part, 16)    return numberdef number_to_ipv6(number):    # Extract each 16-bit block from the number    parts = []    for i in range(8):        parts.insert(0, format(number & 0xFFFF, 'x'))        number >>= 16    # Join the blocks with ':' and handle the shorthand '::'    ipv6 = ':'.join(parts)    # Compress the longest sequence of zero blocks to '::'    ipv6 = ipv6.replace(':0000:', '::', 1)    return ipv6.replace('::0', '::')ipv6 = "2001:0db8:85a3:0000:0000:8a2e:0370:7334"number = ipv6_to_number(ipv6)print(f"IPv6 as number: {number}")# Convert number back to IPv6 address stringrestored_ipv6 = number_to_ipv6(number+1)print(f"Restored IPv6: {restored_ipv6}")

I am struggling to convert the same in JQ function syntax. Can someone please help?

With Regards,

  • M -

Viewing all articles
Browse latest Browse all 522

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>