sanj7573
May 29th, 2008, 04:19 AM
Can Any one help me with a piece of code/function to convert
Hex(for ex #H97626088) to the dotted IP format (xx.xx.xx.xx)
In php I received following...
/**
* Converts a dotquad IP string into a hexidecimal IP string
* @access private
* @param string $user_ip Dot quad variant of the IP address
* @return string Hexadecimal encoding of the dot quad IP address
*/
function encode_ip($user_ip) {
// First thing to do, split the IP into its quads
$ip = explode('.', $user_ip);
// Now we convert it and return it to the caller
return sprintf('%02x%02x%02x%02x', $ip[0], $ip[1], $ip[2], $ip[3]);
}
/**
* Converts a hexidecimal IP string into a dotquad IP string
* @access private
* @param string $enc_ip The hexidecimal encoded IP address
* @return string The dot quad IP address
*/
function decode_ip($enc_ip) {
// First thing with this one is to take the 8 char hex IP and add a dot every two chars, the explode it
$ip_pop = explode('.', chunk_split($enc_ip, 2, '.'));
// Now send back a 'rebuilt' dotquad IP address
return hexdec($ip_pop[0]). '.' . hexdec($ip_pop[1]) . '.' . hexdec($ip_pop[2]) . '.' . hexdec($ip_pop[3]);
}
Thanks in advance
kindly note that i am a fresher ... :wave: :wave:
Hex(for ex #H97626088) to the dotted IP format (xx.xx.xx.xx)
In php I received following...
/**
* Converts a dotquad IP string into a hexidecimal IP string
* @access private
* @param string $user_ip Dot quad variant of the IP address
* @return string Hexadecimal encoding of the dot quad IP address
*/
function encode_ip($user_ip) {
// First thing to do, split the IP into its quads
$ip = explode('.', $user_ip);
// Now we convert it and return it to the caller
return sprintf('%02x%02x%02x%02x', $ip[0], $ip[1], $ip[2], $ip[3]);
}
/**
* Converts a hexidecimal IP string into a dotquad IP string
* @access private
* @param string $enc_ip The hexidecimal encoded IP address
* @return string The dot quad IP address
*/
function decode_ip($enc_ip) {
// First thing with this one is to take the 8 char hex IP and add a dot every two chars, the explode it
$ip_pop = explode('.', chunk_split($enc_ip, 2, '.'));
// Now send back a 'rebuilt' dotquad IP address
return hexdec($ip_pop[0]). '.' . hexdec($ip_pop[1]) . '.' . hexdec($ip_pop[2]) . '.' . hexdec($ip_pop[3]);
}
Thanks in advance
kindly note that i am a fresher ... :wave: :wave: