almost 6 years ago

Today, I need to transform the CIDR into netmask. The key point is the formula of transformation. The rest work are string handling. I record it there. Maybe, I will use it in the future.

def cidr_2_submask(cidr):  
    cidr_in_hex = "%x" % (0xffffffff - ((2 ** (32 - int(cidr)) - 1)))  
    masks = []  
    for x in range(0, 8, 2):  
        masks.append(str(int(cidr_in_hex[x:x + 2], 16)))  
    return ".".join(masks)  
MVC 與 MVVM →
 
comments powered by Disqus