IPAddressHelper.ConvertToLong(string ipv4Address)

This forum is only for questions or discussions about working with the mojoPortal source code in Visual Studio, obtaining the source code from the repository, developing custom features, etc. If your question is not along these lines this is not the right forum. Please try to post your question in the appropriate forum.

Please do not post questions about design, CSS, or skinning here. Use the Help With Skins Forum for those questions.

This forum is for discussing mojoPortal development

This forum is only for questions or discussions about working with the mojoPortal source code in Visual Studio, obtaining the source code from the repository, developing custom features, etc. If your question is not along these lines this is not the right forum. Please try to post your question in the appropriate forum.

You can monitor commits to the repository from this page. We also recommend developers to subscribe to email notifications in the developer forum as occasionally important things are announced.

Before posting questions here you might want to review the developer documentation.

Do not post questions about design, CSS, or skinning here. Use the Help With Skins Forum for those questions.
This thread is closed to new posts. You must sign in to post in the forums.
7/28/2012 2:46:15 PM
Gravatar
Total Posts 7

IPAddressHelper.ConvertToLong(string ipv4Address)

IPAddressHelper.ConvertToLong function (string IPv4Address) is not correct

I modified it like this

for (int i = 0; i <b.Length; i + +)                 

{                     

  result + = (long) (b [i] * Math.pow (256, 3-i));                 

}

it's ok for you?

Norbert

7/29/2012 5:34:40 AM
Gravatar
Total Posts 7

Re: IPAddressHelper.ConvertToLong(string ipv4Address)

this is not good :-(

this works      

 

public static Int64 ConvertToLong(string ipv4Address)

        {      

      Int64 result = 0;

      if (ipv4Address.Contains(":")) { return result; } // an ipv6 address was passed instead

      IPAddress ipAddress;

      if (IPAddress.TryParse(ipv4Address, out ipAddress))

            {

               byte[] b = ipAddress.GetAddressBytes();    

                result  = (long)(b[0] * 16777216);

                result += (long)(b[1] * 65536);

                result += (long)(b[2] * 256);

                result += (long)(b[3] * 1);

            }

return result;

        }

 

Norbert

7/29/2012 7:40:31 AM
Gravatar
Total Posts 18439

Re: IPAddressHelper.ConvertToLong(string ipv4Address)

Hi,

Before submitting a code change please first prove the need for the change. ie prove/show that the existing code is wrong and that the change fixes something. For example provide an ip address that is not correctly converted to a long by the existing code but is correctly converted by the changed code.

Thanks,

Joe

7/30/2012 6:36:52 AM
Gravatar
Total Posts 7

Re: IPAddressHelper.ConvertToLong(string ipv4Address)

 

Bonjour,

resultat du test du code existant et deux autre solutions qui fonctionne:

 

public static Int64 ConvertToLong(string ipv4Address)
{
Int64 result = 0;
ipv4Address = "180.76.5.56";

if (ipv4Address.Contains(":")) { return result; } // an ipv6 address was passed instead

IPAddress ipAddress;

if (IPAddress.TryParse(ipv4Address, out ipAddress))
{
byte[] b = ipAddress.GetAddressBytes();

// old code from Joe

for (int i = 0; i < b.Length; i++)
{
result += (long)(b[i] * Math.Pow(256, i));
}

// ok ci dessous mais ok pour obselet microsoft :-)
Int64 result2 = 0;
result2 = (long)(Math.Pow(256, 3) * b[0]);
result2 += (long)(Math.Pow(256, 2) * b[1]);
result2 += (long)(Math.Pow(256, 1) * b[2]);
result2 += (long)(b[3] * 1);


Int64 result3 = 0;
result3 = (long)(uint)IPAddress.HostToNetworkOrder((int)IPAddress.Parse(ipv4Address).Address);

}

return result;
}

 

result == 939871412; //ko

result2 == 3024880952; //ok

result3 == 3024880952; //ok

I use result3  to  ipligence lite.

best

Norbert

 

7/30/2012 12:49:27 PM
Gravatar
Total Posts 18439

Re: IPAddressHelper.ConvertToLong(string ipv4Address)

Ok, this is now in the source code repository.

Thanks,

Joe

You must sign in to post in the forums. This thread is closed to new posts.