Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6229093
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T09:30:19+00:00 2026-05-24T09:30:19+00:00

The inet_pton function returns an IP address in its packed in_addr representation, i.e. ::1

  • 0

The inet_pton function returns an IP address in its packed in_addr representation, i.e. ::1 becomes “. How can I convert this 16-byte value to an integer? (Well, the string representation of an integer.)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T09:30:20+00:00Added an answer on May 24, 2026 at 9:30 am

    The following code does it:

    $in_addr = inet_pton('21DA:00D3:0000:2F3B:02AA:00FF:FE28:9C5A');
    $unpacked = unpack('a16', $in_addr);
    $unpacked = str_split($unpacked[1]);
    $binary = '';
    foreach ($unpacked as $char) {
        $binary .= str_pad(decbin(ord($char)), 8, '0', STR_PAD_LEFT);
    }
    
    $gmp = gmp_init($binary, 2);
    $str = gmp_strval($gmp);
    

    You can also reverse it with the following code:

    $gmp = gmp_init($str);
    $binary = gmp_strval($gmp, 2);
    $binary = str_pad($binary, 128, '0', STR_PAD_LEFT);
    
    $binary_arr = str_split($binary, 8);
    $packed = '';
    foreach ($binary_arr as $char) {
        $packed .= chr(bindec($char));
    }
    
    $packed = pack('a16', $packed);
    echo inet_ntop($packed);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to implement a function called inet_pton which will convert a string
Using this thing I can obtain original destination IP address of socket(PF_INET, SOCK_DGRAM, 0)
On my Mac, inet_ntop produces this IPv6 address for a certain 128-bit value: 2001::53aa:64c:422:2ece:a29c:9cf6.51391
Why does socket.inet_aton returns packed format in python? If I am storing the IP
Using sockets in C, is it safe to do this in a function? int
Why is the IPv4's decimal value different with inet_pton and inet_addr (1734763876) than what
Possible Duplicates: .NET Equivalant for INET_NTOA and INET_ATON How to convert an IPv4 address
I'm able to store an IP address in the database using the INET_ATON function
I'm successfully storing an IP address in my database using the the INET_NTOA function
When reading from a IO::Socket::INET filehandle it can not be assumed that there will

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.