c#如何将一个用户输入的ip地址,转换成32位有符号整数就好像转换成这样, 谢谢!
0x3c, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 

解决方案 »

  1.   

    System.Net.IPAddress .Parse("").Address 
      

  2.   

    private void button2_Click(object sender, EventArgs e)
            {
                IPAddress test = IPAddress.Parse("127.0.0.1");
                String[] result = new string[4];
                result = test.ToString().Split('.');
                string final = str2hex(result, 4);
                MessageBox.Show(final);
            }
            public String int2hex(uint src)
            {
                String result = "";
                if (src < 10)
                {
                    result = src.ToString();
                }
                else
                {
                    switch (src)
                    {
                        case 10:
                            result = "A";
                            break;
                        case 11:
                            result = "B";
                            break;
                        case 12:
                            result = "C";
                            break;
                        case 13:
                            result = "D";
                            break;
                        case 14:
                            result = "E";
                            break;
                        case 15:
                            result = "F";
                            break;
                    }
                }
                return result;
            }
            public string str2hex(string[] source, int num)
            {
                string temp;
                string result = "";
                for (int str2hexcount = 0; str2hexcount < num; str2hexcount++)
                {
                    String valuetemp = "0x";
                    temp = source[str2hexcount];
                    uint value = Convert.ToUInt32(temp);
                    if (value > 255)
                    {
                        MessageBox.Show("超出范围");
                    }
                    else
                    {
                        uint b = value / 16 % 16;
                        valuetemp = valuetemp + int2hex(b);
                        uint a = value % 16;
                        valuetemp = valuetemp + int2hex(a) + ";";
                        source[str2hexcount] = valuetemp;
                    }            }
                for (int i = 0; i < num; i++)
                {
                    result = result + source[i];
                }
                return result;
            }
      

  3.   

    转换的是127.0.0.1,方法写在按钮事件那个函数里面,有两个子函数str2hex和int2hex
      

  4.   


     我是想把ip地址转换成 0x00, 0x00, 0x03 形式的数组存放到 注册表中的, 这是string格式,应该如何直接得到 byte[] 数组呢??
      

  5.   

    对于每一个相应的uint,左移8位并且与下一个数字|操作。
      

  6.   


      IPAddress ip = IPAddress.Parse("127.0.0.1");
     UInt64 longIP = BitConverter.ToUInt64(ip.GetAddressBytes(), 0);