A Pair of Handy Functions - IP to Hex Conversion
If you’ve done any custom IP work in the phpBB world, then you know that phpBB stores IP addresses in a hex format. This is simple, but you’ll definitely need to convert back to plain IP format (in octets) if you want to display that information. I am sure phpBB probably has some functions for converting between the 2, but it was easier and faster to just write my own. Here they are for your convenient use:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | function ip_to_hex($ip) // Purpose: Takes a user's IP and converts it to the hex value needed by phpBB // Arguements: $ip - The remote user's IP address // Returns: $hex - The user's IP in hex { $pieces = explode(".", $ip); $hex0 = dechex($pieces[0]); $hex1 = dechex($pieces[1]); $hex2 = dechex($pieces[2]); $hex3 = dechex($pieces[3]); if(strlen($hex0) < 2) $hex0 = "0".$hex0; if(strlen($hex1) < 2) $hex1 = "0".$hex1; if(strlen($hex2) < 2) $hex2 = "0".$hex2; if(strlen($hex3) < 2) $hex3 = "0".$hex3; $hex = $hex0 . $hex1 . $hex2 . $hex3; return $hex; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | function hex_to_ip($hex) // Purpose: Takes an IP in hex form and converts it to the plain IP form // Arguements: $hex - The user's IP in hex // Returns: $ip - The remote user's IP address { $pieces[0] = substr($hex, 0, 2); $pieces[1] = substr($hex, 2, 2); $pieces[2] = substr($hex, 4, 2); $pieces[3] = substr($hex, 6, 2); $ip0 = hexdec($pieces[0]); $ip1 = hexdec($pieces[1]); $ip2 = hexdec($pieces[2]); $ip3 = hexdec($pieces[3]); $ip = $ip0.".".$ip1.".".$ip2.".".$ip3; return $ip; } |
01 Nov 2007 02:54 pm Chris 0 comments