将ip地址转换为我们经常看到的4个整数的那种,c#语言,以下是我写的代码...
发布网友
发布时间:2022-04-23 08:40
我来回答
共2个回答
热心网友
时间:2022-04-19 00:16
做个例子,希望有所帮助。代码using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
IPHostEntry ipHostEntry = Dns.GetHostEntry(Dns.GetHostName());
IPAddress addr = new IPAddress(ipHostEntry.AddressList[0].GetAddressBytes());
//"常见的整数"放到字符串数组
string[] strs = addr.ToString().Split('.');
this.textBox1.Text = addr.ToString();
this.textBox2.Text = strs[0];
this.textBox3.Text = strs[1];
this.textBox4.Text = strs[2];
this.textBox5.Text = strs[3];
}
}
}
结果
热心网友
时间:2022-04-19 01:34
public class IPHostTest
{
static void Main(string[] args)
{
IPHostEntry ipHostEntry = Dns.GetHostEntry(Dns.GetHostName());
IPAddress addr = new IPAddress(ipHostEntry.AddressList[0].GetAddressBytes());
// “常见的整数” 放到字符串数组,转换成整数的话 Convert.ToInt32(strs[0]);
string[] strs = addr.ToString().Split('.');
Console.WriteLine("{0}.{1}.{2}.{3}",strs[0],strs[1],strs[2],strs[3]);
}
}