SQL: Binary to IP Address










6















I'm trying to convert A binary IP to a human-readable IP



SELECT HEX( `ip_bin` ) FROM `log_metadata`


gives me
4333D26E000000000000000000000000



And



SELECT INET_NTOA(0x4333D26E)


gives me 67.51.210.110



So I tried:



SELECT
SUBSTRING( CONVERT(HEX(`ip_bin`), CHAR(32)), 1, 8 ) AS `A`
, INET_NTOA(
SUBSTRING( CONVERT(HEX(`ip_bin`), CHAR(32)), 1, 8 )
) AS `B`
, INET_NTOA(hex(`ip_bin`)) AS `C`
, INET_NTOA(`ip_bin`) AS `D`
FROM `log_metadata`


But I only get



+----------+------------+------------+---------+
| A | B | C | D |
+----------+------------+------------+---------+
| 4333D26E | 0.0.16.237 | 0.0.16.237 | 0.0.0.0 |
+----------+------------+------------+---------+


Any suggestions?










share|improve this question






















  • Probably is the missing 0x in front of the string A.

    – Niloct
    Mar 12 '13 at 19:51











  • Tried that. It just returned 0 and 0.0.0.0

    – redolent
    Mar 12 '13 at 19:54















6















I'm trying to convert A binary IP to a human-readable IP



SELECT HEX( `ip_bin` ) FROM `log_metadata`


gives me
4333D26E000000000000000000000000



And



SELECT INET_NTOA(0x4333D26E)


gives me 67.51.210.110



So I tried:



SELECT
SUBSTRING( CONVERT(HEX(`ip_bin`), CHAR(32)), 1, 8 ) AS `A`
, INET_NTOA(
SUBSTRING( CONVERT(HEX(`ip_bin`), CHAR(32)), 1, 8 )
) AS `B`
, INET_NTOA(hex(`ip_bin`)) AS `C`
, INET_NTOA(`ip_bin`) AS `D`
FROM `log_metadata`


But I only get



+----------+------------+------------+---------+
| A | B | C | D |
+----------+------------+------------+---------+
| 4333D26E | 0.0.16.237 | 0.0.16.237 | 0.0.0.0 |
+----------+------------+------------+---------+


Any suggestions?










share|improve this question






















  • Probably is the missing 0x in front of the string A.

    – Niloct
    Mar 12 '13 at 19:51











  • Tried that. It just returned 0 and 0.0.0.0

    – redolent
    Mar 12 '13 at 19:54













6












6








6


2






I'm trying to convert A binary IP to a human-readable IP



SELECT HEX( `ip_bin` ) FROM `log_metadata`


gives me
4333D26E000000000000000000000000



And



SELECT INET_NTOA(0x4333D26E)


gives me 67.51.210.110



So I tried:



SELECT
SUBSTRING( CONVERT(HEX(`ip_bin`), CHAR(32)), 1, 8 ) AS `A`
, INET_NTOA(
SUBSTRING( CONVERT(HEX(`ip_bin`), CHAR(32)), 1, 8 )
) AS `B`
, INET_NTOA(hex(`ip_bin`)) AS `C`
, INET_NTOA(`ip_bin`) AS `D`
FROM `log_metadata`


But I only get



+----------+------------+------------+---------+
| A | B | C | D |
+----------+------------+------------+---------+
| 4333D26E | 0.0.16.237 | 0.0.16.237 | 0.0.0.0 |
+----------+------------+------------+---------+


Any suggestions?










share|improve this question














I'm trying to convert A binary IP to a human-readable IP



SELECT HEX( `ip_bin` ) FROM `log_metadata`


gives me
4333D26E000000000000000000000000



And



SELECT INET_NTOA(0x4333D26E)


gives me 67.51.210.110



So I tried:



SELECT
SUBSTRING( CONVERT(HEX(`ip_bin`), CHAR(32)), 1, 8 ) AS `A`
, INET_NTOA(
SUBSTRING( CONVERT(HEX(`ip_bin`), CHAR(32)), 1, 8 )
) AS `B`
, INET_NTOA(hex(`ip_bin`)) AS `C`
, INET_NTOA(`ip_bin`) AS `D`
FROM `log_metadata`


But I only get



+----------+------------+------------+---------+
| A | B | C | D |
+----------+------------+------------+---------+
| 4333D26E | 0.0.16.237 | 0.0.16.237 | 0.0.0.0 |
+----------+------------+------------+---------+


Any suggestions?







mysql string binary string-formatting






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 12 '13 at 19:47









redolentredolent

2,80432537




2,80432537












  • Probably is the missing 0x in front of the string A.

    – Niloct
    Mar 12 '13 at 19:51











  • Tried that. It just returned 0 and 0.0.0.0

    – redolent
    Mar 12 '13 at 19:54

















  • Probably is the missing 0x in front of the string A.

    – Niloct
    Mar 12 '13 at 19:51











  • Tried that. It just returned 0 and 0.0.0.0

    – redolent
    Mar 12 '13 at 19:54
















Probably is the missing 0x in front of the string A.

– Niloct
Mar 12 '13 at 19:51





Probably is the missing 0x in front of the string A.

– Niloct
Mar 12 '13 at 19:51













Tried that. It just returned 0 and 0.0.0.0

– redolent
Mar 12 '13 at 19:54





Tried that. It just returned 0 and 0.0.0.0

– redolent
Mar 12 '13 at 19:54












4 Answers
4






active

oldest

votes


















4














mysql> select inet_ntoa(conv('4333d26e', 16, 10));
+-------------------------------------+
| inet_ntoa(conv('4333d26e', 16, 10)) |
+-------------------------------------+
| 67.51.210.110 |
+-------------------------------------+
1 row in set (0.00 sec)


Check if it works there too =)



Edit



The problem is that inet_ntoa seems to parse from decimal strings number representation, not hexadecimal ones, or from hexadecimal integers. Compare:



mysql> select inet_ntoa(0x4333d26e);
+-----------------------+
| inet_ntoa(0x4333d26e) |
+-----------------------+
| 67.51.210.110 |
+-----------------------+
1 row in set (0.02 sec)

mysql> select inet_ntoa('0x4333d26e');
+-------------------------+
| inet_ntoa('0x4333d26e') |
+-------------------------+
| 0.0.0.0 |
+-------------------------+
1 row in set, 1 warning (0.00 sec)


Edit



This is simpler and seems to work too:



SELECT INET_NTOA(CONV(ip_bin, 2, 10)) FROM log_metadata





share|improve this answer




















  • 1





    I ended up using INET_NTOA( CONV( SUBSTRING(HEX( ip_bin ), 1, 8), 16, 10 ))

    – redolent
    Mar 12 '13 at 20:09












  • Try this: SELECT INET_NTOA(CONV(ip_bin, 2, 10)) FROM log_metadata

    – Niloct
    Mar 12 '13 at 20:24











  • Didn't work. The problem is that the bits are on the left for some reason - 0x4333D26E000000000000000000000000

    – redolent
    Mar 12 '13 at 23:13











  • You can try shifting those to the right. Mysql has shift operators.

    – Niloct
    Mar 13 '13 at 0:42











  • MySQL seems to hate those, however. My best guess is it parses the value to be integer max

    – redolent
    Mar 14 '13 at 20:51



















3














I found I had to call HEX to convert my binary field to a hex string first, so the following worked for me:



select inet_ntoa(conv(HEX(ip_bin), 16, 10)) from log_metadata





share|improve this answer






























    2














    When using Mysql 5.6.3 or later, it is easier to just use INET6_NTOA - it takes a binary string and returns the human readable format for it. It also supports both IPv4 and IPv6 addresses and returns the format accordingly. So in your example you would use:



    SELECT INET6_NTOA( `ip_bin` ) FROM `log_metadata`


    And should get the human readable result.






    share|improve this answer






























      1














      FYI~ this works in newer version of mysql



       SELECT INET6_NTOA( `ip_bin` ) FROM `log_metadata`


      which is the same as



      SELECT INET_NTOA( CONV( SUBSTRING(HEX(`ip_bin` ), 1, 8), 16, 10 )) FROM log_metadata;





      share|improve this answer






















        Your Answer






        StackExchange.ifUsing("editor", function ()
        StackExchange.using("externalEditor", function ()
        StackExchange.using("snippets", function ()
        StackExchange.snippets.init();
        );
        );
        , "code-snippets");

        StackExchange.ready(function()
        var channelOptions =
        tags: "".split(" "),
        id: "1"
        ;
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function()
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled)
        StackExchange.using("snippets", function()
        createEditor();
        );

        else
        createEditor();

        );

        function createEditor()
        StackExchange.prepareEditor(
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader:
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        ,
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        );



        );













        draft saved

        draft discarded


















        StackExchange.ready(
        function ()
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f15370721%2fsql-binary-to-ip-address%23new-answer', 'question_page');

        );

        Post as a guest















        Required, but never shown

























        4 Answers
        4






        active

        oldest

        votes








        4 Answers
        4






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        4














        mysql> select inet_ntoa(conv('4333d26e', 16, 10));
        +-------------------------------------+
        | inet_ntoa(conv('4333d26e', 16, 10)) |
        +-------------------------------------+
        | 67.51.210.110 |
        +-------------------------------------+
        1 row in set (0.00 sec)


        Check if it works there too =)



        Edit



        The problem is that inet_ntoa seems to parse from decimal strings number representation, not hexadecimal ones, or from hexadecimal integers. Compare:



        mysql> select inet_ntoa(0x4333d26e);
        +-----------------------+
        | inet_ntoa(0x4333d26e) |
        +-----------------------+
        | 67.51.210.110 |
        +-----------------------+
        1 row in set (0.02 sec)

        mysql> select inet_ntoa('0x4333d26e');
        +-------------------------+
        | inet_ntoa('0x4333d26e') |
        +-------------------------+
        | 0.0.0.0 |
        +-------------------------+
        1 row in set, 1 warning (0.00 sec)


        Edit



        This is simpler and seems to work too:



        SELECT INET_NTOA(CONV(ip_bin, 2, 10)) FROM log_metadata





        share|improve this answer




















        • 1





          I ended up using INET_NTOA( CONV( SUBSTRING(HEX( ip_bin ), 1, 8), 16, 10 ))

          – redolent
          Mar 12 '13 at 20:09












        • Try this: SELECT INET_NTOA(CONV(ip_bin, 2, 10)) FROM log_metadata

          – Niloct
          Mar 12 '13 at 20:24











        • Didn't work. The problem is that the bits are on the left for some reason - 0x4333D26E000000000000000000000000

          – redolent
          Mar 12 '13 at 23:13











        • You can try shifting those to the right. Mysql has shift operators.

          – Niloct
          Mar 13 '13 at 0:42











        • MySQL seems to hate those, however. My best guess is it parses the value to be integer max

          – redolent
          Mar 14 '13 at 20:51
















        4














        mysql> select inet_ntoa(conv('4333d26e', 16, 10));
        +-------------------------------------+
        | inet_ntoa(conv('4333d26e', 16, 10)) |
        +-------------------------------------+
        | 67.51.210.110 |
        +-------------------------------------+
        1 row in set (0.00 sec)


        Check if it works there too =)



        Edit



        The problem is that inet_ntoa seems to parse from decimal strings number representation, not hexadecimal ones, or from hexadecimal integers. Compare:



        mysql> select inet_ntoa(0x4333d26e);
        +-----------------------+
        | inet_ntoa(0x4333d26e) |
        +-----------------------+
        | 67.51.210.110 |
        +-----------------------+
        1 row in set (0.02 sec)

        mysql> select inet_ntoa('0x4333d26e');
        +-------------------------+
        | inet_ntoa('0x4333d26e') |
        +-------------------------+
        | 0.0.0.0 |
        +-------------------------+
        1 row in set, 1 warning (0.00 sec)


        Edit



        This is simpler and seems to work too:



        SELECT INET_NTOA(CONV(ip_bin, 2, 10)) FROM log_metadata





        share|improve this answer




















        • 1





          I ended up using INET_NTOA( CONV( SUBSTRING(HEX( ip_bin ), 1, 8), 16, 10 ))

          – redolent
          Mar 12 '13 at 20:09












        • Try this: SELECT INET_NTOA(CONV(ip_bin, 2, 10)) FROM log_metadata

          – Niloct
          Mar 12 '13 at 20:24











        • Didn't work. The problem is that the bits are on the left for some reason - 0x4333D26E000000000000000000000000

          – redolent
          Mar 12 '13 at 23:13











        • You can try shifting those to the right. Mysql has shift operators.

          – Niloct
          Mar 13 '13 at 0:42











        • MySQL seems to hate those, however. My best guess is it parses the value to be integer max

          – redolent
          Mar 14 '13 at 20:51














        4












        4








        4







        mysql> select inet_ntoa(conv('4333d26e', 16, 10));
        +-------------------------------------+
        | inet_ntoa(conv('4333d26e', 16, 10)) |
        +-------------------------------------+
        | 67.51.210.110 |
        +-------------------------------------+
        1 row in set (0.00 sec)


        Check if it works there too =)



        Edit



        The problem is that inet_ntoa seems to parse from decimal strings number representation, not hexadecimal ones, or from hexadecimal integers. Compare:



        mysql> select inet_ntoa(0x4333d26e);
        +-----------------------+
        | inet_ntoa(0x4333d26e) |
        +-----------------------+
        | 67.51.210.110 |
        +-----------------------+
        1 row in set (0.02 sec)

        mysql> select inet_ntoa('0x4333d26e');
        +-------------------------+
        | inet_ntoa('0x4333d26e') |
        +-------------------------+
        | 0.0.0.0 |
        +-------------------------+
        1 row in set, 1 warning (0.00 sec)


        Edit



        This is simpler and seems to work too:



        SELECT INET_NTOA(CONV(ip_bin, 2, 10)) FROM log_metadata





        share|improve this answer















        mysql> select inet_ntoa(conv('4333d26e', 16, 10));
        +-------------------------------------+
        | inet_ntoa(conv('4333d26e', 16, 10)) |
        +-------------------------------------+
        | 67.51.210.110 |
        +-------------------------------------+
        1 row in set (0.00 sec)


        Check if it works there too =)



        Edit



        The problem is that inet_ntoa seems to parse from decimal strings number representation, not hexadecimal ones, or from hexadecimal integers. Compare:



        mysql> select inet_ntoa(0x4333d26e);
        +-----------------------+
        | inet_ntoa(0x4333d26e) |
        +-----------------------+
        | 67.51.210.110 |
        +-----------------------+
        1 row in set (0.02 sec)

        mysql> select inet_ntoa('0x4333d26e');
        +-------------------------+
        | inet_ntoa('0x4333d26e') |
        +-------------------------+
        | 0.0.0.0 |
        +-------------------------+
        1 row in set, 1 warning (0.00 sec)


        Edit



        This is simpler and seems to work too:



        SELECT INET_NTOA(CONV(ip_bin, 2, 10)) FROM log_metadata






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Mar 12 '13 at 20:30

























        answered Mar 12 '13 at 19:59









        NiloctNiloct

        5,39432745




        5,39432745







        • 1





          I ended up using INET_NTOA( CONV( SUBSTRING(HEX( ip_bin ), 1, 8), 16, 10 ))

          – redolent
          Mar 12 '13 at 20:09












        • Try this: SELECT INET_NTOA(CONV(ip_bin, 2, 10)) FROM log_metadata

          – Niloct
          Mar 12 '13 at 20:24











        • Didn't work. The problem is that the bits are on the left for some reason - 0x4333D26E000000000000000000000000

          – redolent
          Mar 12 '13 at 23:13











        • You can try shifting those to the right. Mysql has shift operators.

          – Niloct
          Mar 13 '13 at 0:42











        • MySQL seems to hate those, however. My best guess is it parses the value to be integer max

          – redolent
          Mar 14 '13 at 20:51













        • 1





          I ended up using INET_NTOA( CONV( SUBSTRING(HEX( ip_bin ), 1, 8), 16, 10 ))

          – redolent
          Mar 12 '13 at 20:09












        • Try this: SELECT INET_NTOA(CONV(ip_bin, 2, 10)) FROM log_metadata

          – Niloct
          Mar 12 '13 at 20:24











        • Didn't work. The problem is that the bits are on the left for some reason - 0x4333D26E000000000000000000000000

          – redolent
          Mar 12 '13 at 23:13











        • You can try shifting those to the right. Mysql has shift operators.

          – Niloct
          Mar 13 '13 at 0:42











        • MySQL seems to hate those, however. My best guess is it parses the value to be integer max

          – redolent
          Mar 14 '13 at 20:51








        1




        1





        I ended up using INET_NTOA( CONV( SUBSTRING(HEX( ip_bin ), 1, 8), 16, 10 ))

        – redolent
        Mar 12 '13 at 20:09






        I ended up using INET_NTOA( CONV( SUBSTRING(HEX( ip_bin ), 1, 8), 16, 10 ))

        – redolent
        Mar 12 '13 at 20:09














        Try this: SELECT INET_NTOA(CONV(ip_bin, 2, 10)) FROM log_metadata

        – Niloct
        Mar 12 '13 at 20:24





        Try this: SELECT INET_NTOA(CONV(ip_bin, 2, 10)) FROM log_metadata

        – Niloct
        Mar 12 '13 at 20:24













        Didn't work. The problem is that the bits are on the left for some reason - 0x4333D26E000000000000000000000000

        – redolent
        Mar 12 '13 at 23:13





        Didn't work. The problem is that the bits are on the left for some reason - 0x4333D26E000000000000000000000000

        – redolent
        Mar 12 '13 at 23:13













        You can try shifting those to the right. Mysql has shift operators.

        – Niloct
        Mar 13 '13 at 0:42





        You can try shifting those to the right. Mysql has shift operators.

        – Niloct
        Mar 13 '13 at 0:42













        MySQL seems to hate those, however. My best guess is it parses the value to be integer max

        – redolent
        Mar 14 '13 at 20:51






        MySQL seems to hate those, however. My best guess is it parses the value to be integer max

        – redolent
        Mar 14 '13 at 20:51














        3














        I found I had to call HEX to convert my binary field to a hex string first, so the following worked for me:



        select inet_ntoa(conv(HEX(ip_bin), 16, 10)) from log_metadata





        share|improve this answer



























          3














          I found I had to call HEX to convert my binary field to a hex string first, so the following worked for me:



          select inet_ntoa(conv(HEX(ip_bin), 16, 10)) from log_metadata





          share|improve this answer

























            3












            3








            3







            I found I had to call HEX to convert my binary field to a hex string first, so the following worked for me:



            select inet_ntoa(conv(HEX(ip_bin), 16, 10)) from log_metadata





            share|improve this answer













            I found I had to call HEX to convert my binary field to a hex string first, so the following worked for me:



            select inet_ntoa(conv(HEX(ip_bin), 16, 10)) from log_metadata






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Dec 15 '14 at 16:49









            Brad ParksBrad Parks

            29.5k36165225




            29.5k36165225





















                2














                When using Mysql 5.6.3 or later, it is easier to just use INET6_NTOA - it takes a binary string and returns the human readable format for it. It also supports both IPv4 and IPv6 addresses and returns the format accordingly. So in your example you would use:



                SELECT INET6_NTOA( `ip_bin` ) FROM `log_metadata`


                And should get the human readable result.






                share|improve this answer



























                  2














                  When using Mysql 5.6.3 or later, it is easier to just use INET6_NTOA - it takes a binary string and returns the human readable format for it. It also supports both IPv4 and IPv6 addresses and returns the format accordingly. So in your example you would use:



                  SELECT INET6_NTOA( `ip_bin` ) FROM `log_metadata`


                  And should get the human readable result.






                  share|improve this answer

























                    2












                    2








                    2







                    When using Mysql 5.6.3 or later, it is easier to just use INET6_NTOA - it takes a binary string and returns the human readable format for it. It also supports both IPv4 and IPv6 addresses and returns the format accordingly. So in your example you would use:



                    SELECT INET6_NTOA( `ip_bin` ) FROM `log_metadata`


                    And should get the human readable result.






                    share|improve this answer













                    When using Mysql 5.6.3 or later, it is easier to just use INET6_NTOA - it takes a binary string and returns the human readable format for it. It also supports both IPv4 and IPv6 addresses and returns the format accordingly. So in your example you would use:



                    SELECT INET6_NTOA( `ip_bin` ) FROM `log_metadata`


                    And should get the human readable result.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Oct 18 '17 at 15:26









                    iquitoiquito

                    1,353717




                    1,353717





















                        1














                        FYI~ this works in newer version of mysql



                         SELECT INET6_NTOA( `ip_bin` ) FROM `log_metadata`


                        which is the same as



                        SELECT INET_NTOA( CONV( SUBSTRING(HEX(`ip_bin` ), 1, 8), 16, 10 )) FROM log_metadata;





                        share|improve this answer



























                          1














                          FYI~ this works in newer version of mysql



                           SELECT INET6_NTOA( `ip_bin` ) FROM `log_metadata`


                          which is the same as



                          SELECT INET_NTOA( CONV( SUBSTRING(HEX(`ip_bin` ), 1, 8), 16, 10 )) FROM log_metadata;





                          share|improve this answer

























                            1












                            1








                            1







                            FYI~ this works in newer version of mysql



                             SELECT INET6_NTOA( `ip_bin` ) FROM `log_metadata`


                            which is the same as



                            SELECT INET_NTOA( CONV( SUBSTRING(HEX(`ip_bin` ), 1, 8), 16, 10 )) FROM log_metadata;





                            share|improve this answer













                            FYI~ this works in newer version of mysql



                             SELECT INET6_NTOA( `ip_bin` ) FROM `log_metadata`


                            which is the same as



                            SELECT INET_NTOA( CONV( SUBSTRING(HEX(`ip_bin` ), 1, 8), 16, 10 )) FROM log_metadata;






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 14 '18 at 14:59









                            Mr HeelisMr Heelis

                            1,07111221




                            1,07111221



























                                draft saved

                                draft discarded
















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid


                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.

                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function ()
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f15370721%2fsql-binary-to-ip-address%23new-answer', 'question_page');

                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                這個網誌中的熱門文章

                                How to read a connectionString WITH PROVIDER in .NET Core?

                                In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

                                Museum of Modern and Contemporary Art of Trento and Rovereto