c - convert IP for reverse ip lookup -
i have ip address e.g. char *ip = "192.168.1.13"
, need convert "13.1.168.192"
there proper possibility in c or have myself making tokens , putting them again?
you use inet_pton
convert binary form, making easy reverse, , convert text inet_ntop
.
remember ipv4 address 32-bit unsigned integer. swapping bytes around of easy.
something this:
const char ip[] = "192.168.0.1"; char reversed_ip[inet_addrstrlen]; in_addr_t addr; /* textual address binary format */ inet_pton(af_inet, ip, &addr); /* reverse bytes in binary address */ addr = ((addr & 0xff000000) >> 24) | ((addr & 0x00ff0000) >> 8) | ((addr & 0x0000ff00) << 8) | ((addr & 0x000000ff) << 24); /* , lastly textual representation again */ inet_ntop(af_inet, &addr, reversed_ip, sizeof(reversed_ip));
after code, variable reversed_ip
contains revered address string.
Comments
Post a Comment