Results 1 to 2 of 2

Thread: Hex to IPv4 conversion

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2008
    Posts
    2

    Hex to IPv4 conversion

    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 ...

  2. #2
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Hex to IPv4 conversion

    What is exactly is your question regarding this code?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width