Basic points of knowledge
ip--> integer:
Converts an IP address into an array of bytes
Convert to int by left shift (<<), with (&), or (|)
Integer--> IP:
The integer value is shifted to the right (>>>), 24 digits to the right, and the operator (&) 0xFF, and the resulting number is the first segment of IP.
The integer value is shifted to the right (>>>), 16 digits to the right, and the operator (&) 0xFF, and the resulting number is the second segment of IP.
The integer value is shifted to the right (>>>), 8 digits to the right, and the operator (&) 0xFF, and the resulting number is the third segment of IP.
The integer value is performed with the operator (&) 0xFF, and the resulting number is the fourth segment IP.
Ideas
IP address to int type, for example, IP is "192.168.1.116", equivalent to "." The IP address is divided into 4 parts, each part corresponding to the weight value of 256^3, 256^2, 256, 1, can be
int type to IP address, thinking similar, divided by weight, but with partial string operation
Code
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> #d
Efine LEN typedef unsigned int uint;
/** * String reshaping/UINT Iptint (char *ipstr) {if (ipstr = NULL) return 0;
Char *token;
UINT i = 3, total = 0, cur;
token = strtok (Ipstr, ".");
while (token!= NULL) {cur = atoi (token);
if (cur >= 0 && cur <= 255) {total = cur * POW (256, i);
} I--;
token = strtok (NULL, ".");
Return to total;
/** * Inverse string/void Swapstr (char *str, int begin, int end) {int I, J;
for (i = begin, j = end; I <= J; i + +, J-) {if (Str[i]!= str[j]) {str[i] = Str[i] ^ str[j];
STR[J] = Str[i] ^ str[j];
Str[i] = Str[i] ^ str[j]; /** * Reshaping the IP String */char* Iptstr (UINT ipint) {char *new = (char *) malloc (LEN);
memset (New, ' n ', LEN ');
New[0] = '. ';
Char token[4];
Int bt, ed, Len, cur;
while (ipint) {cur = ipint% 256;
sprintf (token, "%d", cur);
strcat (new, token);
Ipint/= 256;
if (ipint) strcat (New, ".");
Len = strlen (new);
Swapstr (new, 0, len-1); for (bt = ed = 0; ed < Len;) {while (Ed < Len && New[ed]!= '. ')
{ed + +;
} swapstr (New, BT, ED-1);
ed = 1;
BT = Ed;
} new[len-1] = ';
return new;
int main (void) {char ipstr[len], *new;
UINT Ipint;
while (scanf ("%s", Ipstr)!= EOF) {ipint = Iptint (IPSTR);
printf ("%u\n", ipint);
New = Iptstr (Ipint);
printf ("%s\n", new);
return 0;
}