C# 程序如下,思想是用位移运算(符号“<<”、“>>”)。
private void button1_Click(object sender, System.EventArgs e)
{
string str1,str2,str3,str4;
str4 = textBox1.Text;
str1 = str4.Substring(0,str4.IndexOf("."));
str4 = str4.Remove(0,str4.IndexOf(".")+1);
str2 = str4.Substring(0,str4.IndexOf("."));
str4 = str4.Remove(0,str4.IndexOf(".")+1);
str3 = str4.Substring(0,str4.IndexOf("."));
str4 = str4.Remove(0,str4.IndexOf(".")+1);
long int1,int2,int3,int4;
int1 = long.Parse(str1);
int2 = long.Parse(str2);
int3 = long.Parse(str3);
int4 = long.Parse(str4);
int3 = int3<<8;
int2 = int2<<16;
int1 = int1<<24;
long result;
result = int1+int2+int3+int4;
textBox1.Text = result.ToString();
}
程序还没判断输入数据的合法性,楼主自己加上去就可以了。
判断IP地址的合法性可以用System.Net里的IPAddress类型。就是用IPAddress.Parse方法尝试着把一个string转换成IP地址,如果失败,就表示不合法。
如果还有什么疑问可以加我QQ15076723
-----------------------------------
恩。现在把整个程序写好了,贴上来:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Net;
namespace ConvertIP
{
///
/// Form1 的摘要说明。
///
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox textBox2;
///
/// 必需的设计器变量。
///
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
///
/// 清理所有正在使用的资源。
///
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
///
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
///
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.textBox2 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(8, 40);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(168, 23);
this.textBox1.TabIndex = 0;
this.textBox1.Text = "127.0.0.1";
//
// button1
//
this.button1.Location = new System.Drawing.Point(40, 104);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(104, 24);
this.button1.TabIndex = 1;
this.button1.Text = "Go!";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// label1
//
this.label1.Location = new System.Drawing.Point(8, 8);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(168, 23);
this.label1.TabIndex = 2;
this.label1.Text = "将IP地址转换成数字:";
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(8, 72);
this.textBox2.Name = "textBox2";
this.textBox2.ReadOnly = true;
this.textBox2.Size = new System.Drawing.Size(168, 23);
this.textBox2.TabIndex = 3;
this.textBox2.Text = "";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(7, 16);
this.ClientSize = new System.Drawing.Size(192, 136);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.label1);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox1);
this.Name = "Form1";
this.Text = "ConvertIP";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
///
/// 应用程序的主入口点。
///
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Load(object sender, System.EventArgs e)
{
}
private void button1_Click(object sender, System.EventArgs e)
{
textBox1.Enabled = false;
IPAddress InputIP;
try
{
InputIP = IPAddress.Parse(textBox1.Text);
textBox1.Text = InputIP.ToString();
}
catch
{
MessageBox.Show("请输入正确合法的IP地址!","错误");
textBox1.Enabled = true;
return;
}
string str1,str2,str3,str4;
str4 = textBox1.Text;
str1 = str4.Substring(0,str4.IndexOf("."));
str4 = str4.Remove(0,str4.IndexOf(".")+1);
str2 = str4.Substring(0,str4.IndexOf("."));
str4 = str4.Remove(0,str4.IndexOf(".")+1);
str3 = str4.Substring(0,str4.IndexOf("."));
str4 = str4.Remove(0,str4.IndexOf(".")+1);
long int1,int2,int3,int4;
int1 = long.Parse(str1);
int2 = long.Parse(str2);
int3 = long.Parse(str3);
int4 = long.Parse(str4);
int3 = int3<<8;
int2 = int2<<16;
int1 = int1<<24;
long result;
result = int1+int2+int3+int4;
textBox2.Text = result.ToString();
textBox1.Enabled = true;
}
}
}