# include & ltstdlib.h & gt
# include & ltstring.h & gt
/* IP is converted into an integer as a string.
Parameters:
IP in the form of a ribbon string.
Integer after successful intIP conversion.
Return:
If the conversion fails, return 0; Otherwise, return 1.
*/
int strToInt(char strIP[],unsigned long *intIP)
{
int length = strlen(strIP);
int i=0,k=3,part = 0;
* intIP = 0;
for(I = 0; I < = length; i++)
{
if(strIP[i]== ' . '|| strIP[i]=='\0 ')
(* intIP)| =(part & lt; & lt(k - *8)),part = 0;
else if(strIP[I]& gt; = ' 0 ' & amp& ampstrIP[I]& lt; ='9')
part = part * 10+(strIP[I]-' 0 ');
other
Returns 0;
}
Returns1;
}
/* IP is converted into an integer as a string.
Parameters:
Integer IP
StrIP converts IP into string form.
Return:
If the conversion fails, return 0; Otherwise, return 1.
*/
Int intToStr (unsigned long integer intIP, char strIP[])
{
int buf[4],I;
for(I = 0; I<4; i++)
{
buf[I]= intIP % 256;
intIP/= 256;
}
If (intIP! =0)
Returns 0;
other
{
sprintf(strIP," %d.%d.%d.%d ",buf[3],buf[2],buf[ 1],buf[0]);
Returns1;
}
}
int main(int argc,char *argv[])
{
char strIP[ 16];
Unsigned long integer intIP
// ip is saved in ips.txt file, where input redirection is used to facilitate input.
if(freopen("ips.txt "," r ",stdin)! = empty)
{
//Read the string circularly until the end of the file.
while(scanf("%s ",strIP)! =EOF)
{
//word from string IP integer IP
if(strToInt(strIP,& ampintIP))
{
Printf ("string IP to integer: %s (string) = %lu (decimal) = %lX (hexadecimal) \n ",strIP, intIP, intip);
//Reverse the conversion from integer IP to string IP, and verify the correctness of the conversion.
intToStr(intIP,strIP);
Printf ("Integer to string IP:% lu (decimal) = %lX (hexadecimal) = %s (string) \n\n", intIP, intIP, strip);
}
other
{
Printf("%s is not a legal IP! \n\n ",strIP);
}
}
}
other
{
Printf ("Unable to open ip file! \ n ");
}
scanf("%d ",& ampintIP);
System ("suspended");
Returns 0;
}
/*
The string IP is converted into an integer: 202.11(string) = 339637 1 (decimal) = CA700 10 1 (decimal).
Hexadecimal)
Integer is converted into string IP: 339633792 1 (decimal) = ca7001kloc-0/(hexadecimal) = 202.112.1(
String)
/is not a legal IP!
The string IP is converted into an integer: 255.255.255.0 (string) = 4294967040 (decimal) = FFFFFF00 (
Hexadecimal)
Integer to string IP: 4294967040 (decimal) = FFFFFF00 (hexadecimal) = 255.255.255.0.
(string)
The string IP is converted into an integer: 202.112.1.2 (string) = 339637922 (decimal) = CA700 102 (decimal
Hexadecimal)
Integer is converted into string IP: 3396337922 (decimal) = CA700 102 (hexadecimal) = 202.112.1.2 (
String)
/is not a legal IP!
The string IP is converted into an integer: 255.255.255.0 (string) = 4294967040 (decimal) = FFFFFF00 (
Hexadecimal)
Integer to string IP: 4294967040 (decimal) = FFFFFF00 (hexadecimal) = 255.255.255.0.
(string)
The string IP is converted into an integer: 202.112.2.1(string) = 33968 177 (decimal) = CA70020 1 (ten
Hexadecimal)
Integer to string IP: 3396338 177 (decimal) = CA70020 1 (hexadecimal) = 202.11(
String)
/is not a legal IP!
The string IP is converted into an integer: 255.255.255.0 (string) = 4294967040 (decimal) = FFFFFF00 (
Hexadecimal)
Integer to string IP: 4294967040 (decimal) = FFFFFF00 (hexadecimal) = 255.255.255.0.
(string)
*/