Android编程学习笔记之ip2id程序


Android编程学习笔记之ip2id程序,公司一个项目中需要给一系列网络设备分配id号,id是根据ip算出来的,算法如下:

id共3个字节,高字节:从机号:1-31;后两个字节为ip号的最后两个字节.如ip为192.168.0.240的一台设备从机号为31.则id号为31,00,240换算成十进制为2031856.

源码:

  1. package com.id2ip;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.widget.*;  
  6. import android.view.*;  
  7.   
  8. public class id2ip extends Activity {  
  9.     /** Called when the activity is first created. */  
  10.     private TextView text;  
  11.     private Button button;  
  12.       
  13.     @Override  
  14.     public void onCreate(Bundle savedInstanceState) {  
  15.         super.onCreate(savedInstanceState);  
  16.         setContentView(R.layout.main);  
  17.           
  18.         //获得文本框ID   
  19.         text = (TextView)findViewById(R.id.editText1);  
  20.         //获得按钮ID   
  21.         button = (Button)findViewById(R.id.button1);  
  22.         //重载按键监听方法   
  23.         button.setOnClickListener(new Button.OnClickListener()  
  24.         {  
  25.             @Override  
  26.             public void onClick(View v)  
  27.             {  
  28.                 //获得输入框文本   
  29.                 CharSequence str = text.getText();  
  30.                 do  
  31.                 {  
  32.                     //判断输入是否有效   
  33.                     //如果输入位数不为8位,则无效   
  34.                     if (str.length() != 8)  
  35.                     {  
  36.                         text.setText("输入位数必须为8位");  
  37.                         break;  
  38.                     }  
  39.                     //输入的字符不为数字,则无效   
  40.                     int i = 0;  
  41.                     for (i = 0;i < 8;i++)  
  42.                     {  
  43.                         if ((str.charAt(i) < '0') || (str.charAt(i) > '9'))  
  44.                         {  
  45.                             break;  
  46.                         }  
  47.                     }  
  48.                     if (i < 8)  
  49.                     {  
  50.                         text.setText("输入字符必须为数字");  
  51.                         break;  
  52.                     }  
  53.                       
  54.                     String str_temp = str.toString();  
  55.                     //转换为数字   
  56.                     long num = Long.parseLong(str_temp);  
  57.                     //ip2id   
  58.                     short slave_num = (short)(num / 1000000);  
  59.                     num = num % 1000000;  
  60.                     short ip1 = (short)(num / 1000);  
  61.                     num = num % 1000;  
  62.                     short ip0 = (short)num;  
  63.                     long num_temp = ip0;  
  64.                     num_temp |= ip1 << 8;  
  65.                     num_temp |= slave_num << 16;  
  66.                     str_temp = Long.toString(num_temp);  
  67.                     str = str_temp;  
  68.                     text.setText(str);        
  69.                 }while (false);  
  70.             }  
  71.         });  
  72.           
  73.     }  
  74. }  

注意:

程序中需要注意的地方有3处:

一是字符串转数字,可以用方法Long.parseLong();

二是在android中常用的捕捉空间字符串的类是CharSequence,而java中常用的字符串类为String,则需要转换.

1.CharSequence转String

CharSequence str;

String str_temp = str.toString();

2.String转CharSequence这个直接等于就可以了:str = str_temp;

三是java中没有无符号即unsigned类型,所有类型都是带符号的

 

相关内容