c - How to convert netmask to network prefix length? -
i doing programming, wanna convert netmask network prefix length.
for example 255.255.255.0 ----> 24.
finally write code so.
const char *network = "255.255.255.0"; int n = inet_addr(netowrk); int = 0; while (n > 0) { n = n << 1; i++;
}
i network count
you should first try compile code, can lot. there compilations errors because mistyped variable name "netowrk"
to calculate prefix instead left shift should try right shift , instead of using inet_addr try inet_pton().
for more details go through post ipv4 decimal different values?
here can check code:
int main() { const char *network = "255.255.255.0"; int n; inet_pton(af_inet, network, &n); int = 0; while (n > 0) { n = n >> 1; i++; } printf("network = %s, suffix = %d\n", network, i); }
Comments
Post a Comment