christa Posted July 13, 2009 Share Posted July 13, 2009 hi all why this function return 0 and NOT 3573956936? $ip = '213.6.65.72'; function ip2dec($ip) { $base=explode(".", $ip); $decimal=(double)$base[0]*16777216; $decimal+=$base[1]*65536; $decimal+=$base[2]*256; $decimal+=$base[3]; if($decimal>2147483647) { $decimal-=4294967296; } return (int)$decimal; } echo ip2dec(); Quote Link to comment https://forums.phpfreaks.com/topic/165845-convert-ip-to-dottless-decimal/ Share on other sites More sharing options...
Mchl Posted July 13, 2009 Share Posted July 13, 2009 ip2long Quote Link to comment https://forums.phpfreaks.com/topic/165845-convert-ip-to-dottless-decimal/#findComment-874777 Share on other sites More sharing options...
PFMaBiSmAd Posted July 13, 2009 Share Posted July 13, 2009 And you've got to pass the function a value when you call it. 0 in = 0 out. Quote Link to comment https://forums.phpfreaks.com/topic/165845-convert-ip-to-dottless-decimal/#findComment-874778 Share on other sites More sharing options...
christa Posted July 13, 2009 Author Share Posted July 13, 2009 ip2long now, it returns: -721010360 And you've got to pass the function a value when you call it. 0 in = 0 out. how can i call my function to display the correct ip dottless? Quote Link to comment https://forums.phpfreaks.com/topic/165845-convert-ip-to-dottless-decimal/#findComment-874780 Share on other sites More sharing options...
Mchl Posted July 13, 2009 Share Posted July 13, 2009 See Example #2 in the manual. $ip = '213.6.65.72'; echo ip2dec($ip); Quote Link to comment https://forums.phpfreaks.com/topic/165845-convert-ip-to-dottless-decimal/#findComment-874782 Share on other sites More sharing options...
nbarone Posted July 13, 2009 Share Posted July 13, 2009 And you've got to pass the function a value when you call it. 0 in = 0 out. how can i call my function to display the correct ip dottless? dotless? str_replace(".","",$ip); however, what they said was if you do function(); it's the same as function(0); which in turn, 0 "mathed" against anything will return 0 (which is why you get 0!) Quote Link to comment https://forums.phpfreaks.com/topic/165845-convert-ip-to-dottless-decimal/#findComment-874783 Share on other sites More sharing options...
christa Posted July 13, 2009 Author Share Posted July 13, 2009 however, what they said was if you do function(); it's the same as function(0); which in turn, 0 "mathed" against anything will return 0 (which is why you get 0!) and so, how can i convert my ip into decimal value? Quote Link to comment https://forums.phpfreaks.com/topic/165845-convert-ip-to-dottless-decimal/#findComment-874791 Share on other sites More sharing options...
nbarone Posted July 13, 2009 Share Posted July 13, 2009 by actually giving the function an IP... last line of your code: echo $ip2dec(); should be: echo $ip2dec($ip); Quote Link to comment https://forums.phpfreaks.com/topic/165845-convert-ip-to-dottless-decimal/#findComment-874793 Share on other sites More sharing options...
christa Posted July 13, 2009 Author Share Posted July 13, 2009 by actually giving the function an IP... last line of your code: echo $ip2dec(); should be: echo $ip2dec($ip); i tried but the result is: -721010360 PS: i've seen the funtion on http://www.php.net/manual/en/function.gethostbyaddr.php#25373 Quote Link to comment https://forums.phpfreaks.com/topic/165845-convert-ip-to-dottless-decimal/#findComment-874796 Share on other sites More sharing options...
J.Daniels Posted July 13, 2009 Share Posted July 13, 2009 It is because of this part of the code: if ($decimal > 2147483647) { $decimal -= 4294967296; } The number I get right before this is 3573956936 Quote Link to comment https://forums.phpfreaks.com/topic/165845-convert-ip-to-dottless-decimal/#findComment-874801 Share on other sites More sharing options...
Mchl Posted July 13, 2009 Share Posted July 13, 2009 $ip = '213.6.65.72'; printf("%u\n",ip2long($ip)); long integer is signed in PHP, you have to convert it to string to get unsigned value. Quote Link to comment https://forums.phpfreaks.com/topic/165845-convert-ip-to-dottless-decimal/#findComment-874803 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.