An example of IP2ID program for Android programming _android

Source: Internet
Author: User

This example describes the IP2ID program for Android programming. Share to everyone for your reference. The specific analysis is as follows:

One, Description:

Company a project needs to give a series of network equipment allocation ID number, ID is based on IP, the algorithm is as follows:

ID A total of 3 bytes, high byte: From the machine number: 1-31; the last two bytes are the final two bytes of the IP number. A device with an IP of 192.168.0.240 is 31. The ID number is 31, and the 00,240 is converted to decimal 2031856.

Second, the source code:

Package com.id2ip; 
Import android.app.Activity; 
Import Android.os.Bundle; 
Import android.widget.*; 
Import android.view.*; 
 The public class Id2ip extends activity {/** called the ' when ' is the ' The activity ' is a-created-private TextView text; 
 Private button button; 
 @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); 
 Setcontentview (R.layout.main); 
 Gets the text box id text = (TextView) Findviewbyid (R.ID.EDITTEXT1); 
 Get button ID buttons = (button) Findviewbyid (R.id.button1); 
  Overload key Listener Method Button.setonclicklistener (new Button.onclicklistener () {@Override public void OnClick (View v) { 
  Get input Box text charsequence str = Text.gettext (); 
   Do {//judge whether the input is valid///if the input digits are not 8 digits, then invalid if (Str.length ()!= 8) {text.settext ("input digits must be 8 bits"); 
   Break 
   }//input character is not a number, then invalid int i = 0; for (i = 0;i < 8;i++) {if (Str.charat (i) < ' 0 ') | | 
   (Str.charat (i) > ' 9 ')) 
   {break; } if (I < 8) {text.settext ("input character must be a number"); 
   Break 
   String str_temp = str.tostring (); 
   Convert to number long num = Long.parselong (str_temp); 
   Ip2id Short slave_num = (short) (num/1000000); 
   num = num% 1000000; 
   Short ip1 = (short) (num/1000); 
   num = num% 1000; 
   Short ip0 = (short) num; 
   Long num_temp = Ip0; 
   Num_temp |= ip1 << 8; 
   Num_temp |= slave_num << 16; 
   Str_temp = long.tostring (num_temp); 
   str = str_temp; 
  Text.settext (str); 
  }while (FALSE); 
 } 
 }); } 
}

Third, Attention:

There are 3 places to note in the program:

① string numbers, you can use the method Long.parselong ();

The ② class used to capture space strings in Android is Charsequence, and the common string class in Java is a string that requires conversion.

1.CharSequence Turn string

Charsequence str;
String str_temp = str.tostring ();

2.String to charsequence this directly equals on it:

str = str_temp;

There are no unsigned, unsigned types in ③java, all types are signed

I hope this article will help you with your Android program.

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.