tibberous Posted March 23, 2010 Share Posted March 23, 2010 I use this function to store ip address in an int field in the database: function ip2num($ip){ $parts = explode('.', $ip); $value = 0; $exp = 0; foreach($parts as $part) $value += $part * pow(256, $exp++); return $value; } How do I convert back? Quote Link to comment https://forums.phpfreaks.com/topic/196246-bonus-question-p/ Share on other sites More sharing options...
TeddyKiller Posted March 23, 2010 Share Posted March 23, 2010 Use CODE or PHP tags. What do you mean "Convert back" ? Quote Link to comment https://forums.phpfreaks.com/topic/196246-bonus-question-p/#findComment-1030585 Share on other sites More sharing options...
tibberous Posted March 23, 2010 Author Share Posted March 23, 2010 I got it! I took the number an used base_convert($num, 10, 16) to get a series of 8 alpha-numeric characters. Then, I used windows calculator to convert them from base-16 to base-10 2 at a time to get base 256. But, for some reason, the sets are backwords. I only found this out b/c I was playing with my ip address. So, basically, if you get: 75.149.00.109 You just make it: 109.00.149.75 So, I kind of solved it, but there is a lot more standard way that just involves modding and dividing. Does anyone know that way? @Teddy: It converts an ip address to a base-10 number, so converting back would be getting the original ip. Quote Link to comment https://forums.phpfreaks.com/topic/196246-bonus-question-p/#findComment-1030589 Share on other sites More sharing options...
Daniel0 Posted March 23, 2010 Share Posted March 23, 2010 Uh... ip2long, long2ip Quote Link to comment https://forums.phpfreaks.com/topic/196246-bonus-question-p/#findComment-1030591 Share on other sites More sharing options...
ignace Posted March 23, 2010 Share Posted March 23, 2010 Uh... ip2long, long2ip Makes you wonder why those boys at php.net go through all the trouble?! Quote Link to comment https://forums.phpfreaks.com/topic/196246-bonus-question-p/#findComment-1030623 Share on other sites More sharing options...
oni-kun Posted March 23, 2010 Share Posted March 23, 2010 Uh... ip2long, long2ip Makes you wonder why those boys at php.net go through all the trouble?! And girls. I think? Quote Link to comment https://forums.phpfreaks.com/topic/196246-bonus-question-p/#findComment-1030628 Share on other sites More sharing options...
tibberous Posted March 23, 2010 Author Share Posted March 23, 2010 Lol. Guess my class on C++ bit-encoding was a waste? Quote Link to comment https://forums.phpfreaks.com/topic/196246-bonus-question-p/#findComment-1030714 Share on other sites More sharing options...
ignace Posted March 23, 2010 Share Posted March 23, 2010 Guess my class on C++ bit-encoding was a waste? Not if you are writing C++ Quote Link to comment https://forums.phpfreaks.com/topic/196246-bonus-question-p/#findComment-1030759 Share on other sites More sharing options...
Daniel0 Posted March 23, 2010 Share Posted March 23, 2010 They're in MySQL too, by the way: INET_NTOA(), INET_NTOA() Quote Link to comment https://forums.phpfreaks.com/topic/196246-bonus-question-p/#findComment-1030789 Share on other sites More sharing options...
TeddyKiller Posted March 23, 2010 Share Posted March 23, 2010 They're in MySQL too, by the way: INET_NTOA(), INET_NTOA() There the same? Quote Link to comment https://forums.phpfreaks.com/topic/196246-bonus-question-p/#findComment-1030817 Share on other sites More sharing options...
Mchl Posted March 23, 2010 Share Posted March 23, 2010 No. They just do the same thing. There's exactly one way to convert IP in form of four octets into a 32bit integer. BTW: Make sure you use UNSIGNED INTEGER in MySQL for storing IPs. [edit] OC the second one is INET_ATON() Quote Link to comment https://forums.phpfreaks.com/topic/196246-bonus-question-p/#findComment-1030818 Share on other sites More sharing options...
Daniel0 Posted March 23, 2010 Share Posted March 23, 2010 Heh... yeah the second one (or first one) was supposed to have been INET_ATON(). Quote Link to comment https://forums.phpfreaks.com/topic/196246-bonus-question-p/#findComment-1030819 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.