method to convert an IP address to an int type using Java code _java

Source: Internet
Author: User
Tags strtok

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; 

 }

Related Article

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.