Convert an IP address to an integer

Source: Internet
Author: User
Tags strtok

#include <iostream>#include<math.h>using namespacestd;int main () {Charip[ -]; Chartemp[4][4]; CIN>>IP; Char*delim=".";//Separators    Char*p; unsignedLongI=3;//It's important to keep the string counter-stored here.strcpy (Temp[i--],strtok (Ip,delim));//The strtok string is split into fragments. The parameter S points to the string to be split, and Delim is the delimiter.     while(p=strtok (Null,delim)) {strcpy (temp[i--],p); } unsignedLongarr[4]; unsignedLongstrtemp[4][8];  for(i=0; i<=3; i++)//Convert each string to a number, respectively{Arr[i]=atoi (Temp[i]);//converts the value of a string to an integercout<<arr[i]<<" "; } cout<<Endl;  for(i=0; i<=3; i++)    {      for(unsignedLongj=0; j<=7; j + +)//convert decimal into binary{Strtemp[i][j]=arr[i]%2; Arr[i]=arr[i]/2; cout<<Strtemp[i][j]; } cout<<Endl; } unsignedLongsum=0, result;unsignedLong(*Q) [8]; //here must pay attention to the type of sum, I began to write a long type, the final result is negative, let me debug for a long time!! q=strtemp; unsignedLongCount=0;  for(i=0; i<=3; i++)    {      for(unsignedLongj=0; j<=7; j + +) {result=LDEXP (* (* (q+i) +j), count++); Sum+=result; }} cout<<sum<<Endl; return 0;}

Convert an IP address to an integer

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.