在C++中有这样的函数可以进行转换,在C#我怎么转也转不过来,请高手帮忙,谢谢了!
此函数可将IP地址字符串转换为DWORD
如:DWORD dwAddress = MyStringToDWORD("192.168.0.1");
DWORD MyStringToDWORD(LPCTSTR szAddress)
{
DWORD a1 = 0;
DWORD a2 = 0;
DWORD a3 = 0;
DWORD a4 = 0;
    DWORD r = 0;
sscanf(szAddress, "%d.%d.%d.%d", &a1, &a2, &a3, &a4);
    r = (a1 << 24 ) | (a2 << 16) | (a3 << 8) | a4;
return r;
}

解决方案 »

  1.   

    用IPAddress这个类,它的属性Address是个Long值,至于单个值可以用GetAddressBytes这个方法
      

  2.   

    string ip = "192.168.0.1"; 
    long l = (long)BitConverter.ToUInt32(IPAddress.Parse(ip).GetAddressBytes(), 0);
      

  3.   

           #region 获取IP地址
            public static string GetIpAddress()
            {
                string result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
                if (null == result || result == String.Empty)
                {
                    result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
                }             if (null == result || result == String.Empty)
                {
                    result = HttpContext.Current.Request.UserHostAddress;
                }
                return result;
            } 
            #endregion 获取IP地址        然后做个转化成你需要的格式
      

  4.   


    为什么还要转了,不是直接有Address属性吗,直接用即可
      

  5.   


    得出16844522,
    我使用 string b = Convert.ToString(l, 16);转换出来得到的值还是不对。
      

  6.   

    而且用这个方法转出来,我ping出来IP倒着出来了,比如234.6.1.1,用你的方法转出来,ping出来就是1.1.6.234
    我要求得出来的值是0xEA060101这样子的
      

  7.   

    to 而且用这个方法转出来,我ping出来IP倒着出来了,比如234.6.1.1,用你的方法转出来,ping出来就是1.1.6.234
    我要求得出来的值是0xEA060101这样子的拿到byte[],然后reverse一下
    string ip = "192.168.0.1";
    byte[] bIP = IPAddress.Parse(ip).GetAddressBytes();
    Array.Reverse(bIP);
    uint uIP = BitConverter.ToUInt32(bIP, 0);
    MessageBox.Show(uIP.ToString("X")) ;
      

  8.   

    我现在转过来了,可是现在遇到个麻烦事, uint b4 = 0xEA060101;这样在C#中是允许的,可是如果string a = "0xEA060101";想把a转成uint格式的话,我怎么转也转不过来,不知道有什么好办法!
      

  9.   

    uint u = Convert.ToUInt32("0xEA060101", 16);
      

  10.   


    Knight94 的是对的,16进制是0xEA060101,你转换出来了,但是我想得到uint b4 = 0xEA060101这样的效果,难道在C#只能这样直接赋值吗?有没有办法string a = "0xEA060101";这样的,转换成uint,在C++是可以的,可是在C#当中我搞了一下午了,都不行!
      

  11.   

    你可以做一个实验            string ip = "234.6.1.1";
                long l = (long)BitConverter.ToUInt32(IPAddress.Parse(ip).GetAddressBytes(), 0);
                Console.WriteLine(l.ToString());    //显示是16844522            
                IPAddress ipadress = new IPAddress((long)(0xEA060101));   //你的串
                Console.WriteLine(ipadress.ToString());    // 1.1.6.234            ipadress = new IPAddress((long)16844522);
                Console.WriteLine(ipadress.ToString());   //显示234.6.1.1说明我的是对的,你的是反的.
      

  12.   

            private void button2_Click(object sender, EventArgs e)
            {
                IPAddress ip = IPAddress.Parse(txtInStr.Text);    //txtInStr控件输入 1.1.6.234等合格IP(该处没有检查是否合法)
                txtOutStr.Text = "0x" + ip.Address.ToString("X"); //txtOutStr控件输出 0xEA060101 字符串。
            }        想要用到(IPAddress )
            using System.Net;
      

  13.   


    string ip="234.6.1.1";
    string[] ippart=ip.split('.');
    uint result=i=0;
    while(i<ippart.length)
       result |= Convert.ToInt32(ippart[i])<<8);
      

  14.   


    string ip="234.6.1.1";
    string[] ippart=ip.split('.');
    uint result=i=0;
    while(i<ippart.length)
       result = result<<8 | Convert.ToInt32(ippart[i]);搞错了,应该是这样。
      

  15.   


    string ip="234.6.1.1";
    string[] ippart=ip.split('.');
    uint result=i=0;
    while(i<ippart.length)
       result = result<<8 | Convert.ToInt32(ippart[i++]);搞错了,上面忘了++,应该是这样=.=|!