c/decip4.c: Finally, the ability to turn an IPv4 address into an integer
This commit is contained in:
parent
4d488fccda
commit
51daae4c1d
|
@ -0,0 +1,26 @@
|
|||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
|
||||
uint32_t numeric_ip4(const char *ip) {
|
||||
uint32_t octets = 0;
|
||||
uint8_t octet = 0;
|
||||
|
||||
for(; *ip; ip += 1) {
|
||||
if(*ip == '.') {
|
||||
octets = (octets << 8) | octet;
|
||||
octet = 0;
|
||||
} else {
|
||||
octet = (octet * 10) + (*ip - '0');
|
||||
}
|
||||
}
|
||||
|
||||
return (octets << 8) | octet;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
for(argv += 1, argc -= 1; *argv; argv += 1, argc -= 1) {
|
||||
printf("%s: %u\n", *argv, numeric_ip4(*argv));
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue