Convert hexadecimal string to IP Address












11














I want to convert a string value (in hexadecimal) to an IP Address. How can I do it using Java?



Hex value: 0A064156



IP: 10.6.65.86



This site gives me the correct result, but I am not sure how to implement this in my code.



Can it be done directly in an XSLT?










share|improve this question




















  • 1




    1. Split the string into substrings of length 2. 2. Convert all substrings to dezimal. 3. Insert dots between all substrings.
    – Sirko
    Jul 5 '13 at 12:55
















11














I want to convert a string value (in hexadecimal) to an IP Address. How can I do it using Java?



Hex value: 0A064156



IP: 10.6.65.86



This site gives me the correct result, but I am not sure how to implement this in my code.



Can it be done directly in an XSLT?










share|improve this question




















  • 1




    1. Split the string into substrings of length 2. 2. Convert all substrings to dezimal. 3. Insert dots between all substrings.
    – Sirko
    Jul 5 '13 at 12:55














11












11








11


3





I want to convert a string value (in hexadecimal) to an IP Address. How can I do it using Java?



Hex value: 0A064156



IP: 10.6.65.86



This site gives me the correct result, but I am not sure how to implement this in my code.



Can it be done directly in an XSLT?










share|improve this question















I want to convert a string value (in hexadecimal) to an IP Address. How can I do it using Java?



Hex value: 0A064156



IP: 10.6.65.86



This site gives me the correct result, but I am not sure how to implement this in my code.



Can it be done directly in an XSLT?







java ip hex converters






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 11:47

























asked Jul 5 '13 at 12:53









Rg90

3283826




3283826








  • 1




    1. Split the string into substrings of length 2. 2. Convert all substrings to dezimal. 3. Insert dots between all substrings.
    – Sirko
    Jul 5 '13 at 12:55














  • 1




    1. Split the string into substrings of length 2. 2. Convert all substrings to dezimal. 3. Insert dots between all substrings.
    – Sirko
    Jul 5 '13 at 12:55








1




1




1. Split the string into substrings of length 2. 2. Convert all substrings to dezimal. 3. Insert dots between all substrings.
– Sirko
Jul 5 '13 at 12:55




1. Split the string into substrings of length 2. 2. Convert all substrings to dezimal. 3. Insert dots between all substrings.
– Sirko
Jul 5 '13 at 12:55












6 Answers
6






active

oldest

votes


















16














try this



InetAddress a = InetAddress.getByAddress(DatatypeConverter.parseHexBinary("0A064156"));


DatatypeConverter is from standard javax.xml.bind package






share|improve this answer



















  • 1




    +1. I really like this answer. Not a lot of code or string manipulation and gives you exactly what you want.
    – ARC
    Jul 5 '13 at 13:15










  • Thanks a lot! Sweet and precise.
    – Rg90
    Jul 5 '13 at 13:45










  • Note: Since Java 9 DatatypeConverter is no longer part of the standard: github.com/http-kit/http-kit/issues/356
    – BullyWiiPlaza
    Jun 7 '18 at 21:32



















5














You can split your hex value in groups of 2 and then convert them to integers.




0A = 10



06 = 06



65 = 41



86 = 56




Code:



String hexValue = "0A064156";
String ip = "";

for(int i = 0; i < hexValue.length(); i = i + 2) {
ip = ip + Integer.valueOf(hexValue.subString(i, i+2), 16) + ".";
}

System.out.println("Ip = " + ip);


Output:




Ip = 10.6.65.86.







share|improve this answer

















  • 1




    plus voted,Your answer actually teaches how to catch fish
    – HRgiger
    Oct 14 '15 at 11:21












  • Good answer, just a typo. It should be hexValue.substring. docs.oracle.com/javase/7/docs/api/java/lang/…
    – Paul
    Nov 15 '18 at 1:02



















0














public String convertHexToString(String hex){

StringBuilder sb = new StringBuilder();
StringBuilder temp = new StringBuilder();

for( int i=0; i<hex.length()-1; i+=2 ){


String output = hex.substring(i, (i + 2));

int decimal = Integer.parseInt(output, 16);

sb.append((char)decimal);

temp.append(decimal);
temp.append(".");
}
System.out.println("Decimal : " + temp.toString());

return sb.toString();


}






share|improve this answer





























    0














    You can use the following method:



    public static String convertHexToIP(String hex)
    {
    String ip= "";

    for (int j = 0; j < hex.length(); j+=2) {
    String sub = hex.substring(j, j+2);
    int num = Integer.parseInt(sub, 16);
    ip += num+".";
    }

    ip = ip.substring(0, ip.length()-1);
    return ip;
    }





    share|improve this answer





























      0














      The accepted answer has a requirement that, the hex must be even-length.
      Here is my answer:



      private String getIpByHex(String hex) {
      Long ipLong = Long.parseLong(hex, 16);
      String ipString = String.format("%d.%d.%d.%d", ipLong >> 24,
      ipLong >> 16 & 0x00000000000000FF,
      ipLong >> 8 & 0x00000000000000FF,
      ipLong & 0x00000000000000FF);

      return ipString;
      }





      share|improve this answer





























        -1














        You can split it 2 characters, and then use Integer.parse(string, radix) to convert to integer values



        http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Integer.html#parseInt(java.lang.String,int)






        share|improve this answer





















        • The link is dead.
          – user2796515
          Oct 10 '18 at 15:02











        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%2f17489404%2fconvert-hexadecimal-string-to-ip-address%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        6 Answers
        6






        active

        oldest

        votes








        6 Answers
        6






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        16














        try this



        InetAddress a = InetAddress.getByAddress(DatatypeConverter.parseHexBinary("0A064156"));


        DatatypeConverter is from standard javax.xml.bind package






        share|improve this answer



















        • 1




          +1. I really like this answer. Not a lot of code or string manipulation and gives you exactly what you want.
          – ARC
          Jul 5 '13 at 13:15










        • Thanks a lot! Sweet and precise.
          – Rg90
          Jul 5 '13 at 13:45










        • Note: Since Java 9 DatatypeConverter is no longer part of the standard: github.com/http-kit/http-kit/issues/356
          – BullyWiiPlaza
          Jun 7 '18 at 21:32
















        16














        try this



        InetAddress a = InetAddress.getByAddress(DatatypeConverter.parseHexBinary("0A064156"));


        DatatypeConverter is from standard javax.xml.bind package






        share|improve this answer



















        • 1




          +1. I really like this answer. Not a lot of code or string manipulation and gives you exactly what you want.
          – ARC
          Jul 5 '13 at 13:15










        • Thanks a lot! Sweet and precise.
          – Rg90
          Jul 5 '13 at 13:45










        • Note: Since Java 9 DatatypeConverter is no longer part of the standard: github.com/http-kit/http-kit/issues/356
          – BullyWiiPlaza
          Jun 7 '18 at 21:32














        16












        16








        16






        try this



        InetAddress a = InetAddress.getByAddress(DatatypeConverter.parseHexBinary("0A064156"));


        DatatypeConverter is from standard javax.xml.bind package






        share|improve this answer














        try this



        InetAddress a = InetAddress.getByAddress(DatatypeConverter.parseHexBinary("0A064156"));


        DatatypeConverter is from standard javax.xml.bind package







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Jul 5 '13 at 13:04

























        answered Jul 5 '13 at 12:57









        Evgeniy Dorofeev

        105k23140219




        105k23140219








        • 1




          +1. I really like this answer. Not a lot of code or string manipulation and gives you exactly what you want.
          – ARC
          Jul 5 '13 at 13:15










        • Thanks a lot! Sweet and precise.
          – Rg90
          Jul 5 '13 at 13:45










        • Note: Since Java 9 DatatypeConverter is no longer part of the standard: github.com/http-kit/http-kit/issues/356
          – BullyWiiPlaza
          Jun 7 '18 at 21:32














        • 1




          +1. I really like this answer. Not a lot of code or string manipulation and gives you exactly what you want.
          – ARC
          Jul 5 '13 at 13:15










        • Thanks a lot! Sweet and precise.
          – Rg90
          Jul 5 '13 at 13:45










        • Note: Since Java 9 DatatypeConverter is no longer part of the standard: github.com/http-kit/http-kit/issues/356
          – BullyWiiPlaza
          Jun 7 '18 at 21:32








        1




        1




        +1. I really like this answer. Not a lot of code or string manipulation and gives you exactly what you want.
        – ARC
        Jul 5 '13 at 13:15




        +1. I really like this answer. Not a lot of code or string manipulation and gives you exactly what you want.
        – ARC
        Jul 5 '13 at 13:15












        Thanks a lot! Sweet and precise.
        – Rg90
        Jul 5 '13 at 13:45




        Thanks a lot! Sweet and precise.
        – Rg90
        Jul 5 '13 at 13:45












        Note: Since Java 9 DatatypeConverter is no longer part of the standard: github.com/http-kit/http-kit/issues/356
        – BullyWiiPlaza
        Jun 7 '18 at 21:32




        Note: Since Java 9 DatatypeConverter is no longer part of the standard: github.com/http-kit/http-kit/issues/356
        – BullyWiiPlaza
        Jun 7 '18 at 21:32













        5














        You can split your hex value in groups of 2 and then convert them to integers.




        0A = 10



        06 = 06



        65 = 41



        86 = 56




        Code:



        String hexValue = "0A064156";
        String ip = "";

        for(int i = 0; i < hexValue.length(); i = i + 2) {
        ip = ip + Integer.valueOf(hexValue.subString(i, i+2), 16) + ".";
        }

        System.out.println("Ip = " + ip);


        Output:




        Ip = 10.6.65.86.







        share|improve this answer

















        • 1




          plus voted,Your answer actually teaches how to catch fish
          – HRgiger
          Oct 14 '15 at 11:21












        • Good answer, just a typo. It should be hexValue.substring. docs.oracle.com/javase/7/docs/api/java/lang/…
          – Paul
          Nov 15 '18 at 1:02
















        5














        You can split your hex value in groups of 2 and then convert them to integers.




        0A = 10



        06 = 06



        65 = 41



        86 = 56




        Code:



        String hexValue = "0A064156";
        String ip = "";

        for(int i = 0; i < hexValue.length(); i = i + 2) {
        ip = ip + Integer.valueOf(hexValue.subString(i, i+2), 16) + ".";
        }

        System.out.println("Ip = " + ip);


        Output:




        Ip = 10.6.65.86.







        share|improve this answer

















        • 1




          plus voted,Your answer actually teaches how to catch fish
          – HRgiger
          Oct 14 '15 at 11:21












        • Good answer, just a typo. It should be hexValue.substring. docs.oracle.com/javase/7/docs/api/java/lang/…
          – Paul
          Nov 15 '18 at 1:02














        5












        5








        5






        You can split your hex value in groups of 2 and then convert them to integers.




        0A = 10



        06 = 06



        65 = 41



        86 = 56




        Code:



        String hexValue = "0A064156";
        String ip = "";

        for(int i = 0; i < hexValue.length(); i = i + 2) {
        ip = ip + Integer.valueOf(hexValue.subString(i, i+2), 16) + ".";
        }

        System.out.println("Ip = " + ip);


        Output:




        Ip = 10.6.65.86.







        share|improve this answer












        You can split your hex value in groups of 2 and then convert them to integers.




        0A = 10



        06 = 06



        65 = 41



        86 = 56




        Code:



        String hexValue = "0A064156";
        String ip = "";

        for(int i = 0; i < hexValue.length(); i = i + 2) {
        ip = ip + Integer.valueOf(hexValue.subString(i, i+2), 16) + ".";
        }

        System.out.println("Ip = " + ip);


        Output:




        Ip = 10.6.65.86.








        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jul 5 '13 at 13:00









        JREN

        2,83022142




        2,83022142








        • 1




          plus voted,Your answer actually teaches how to catch fish
          – HRgiger
          Oct 14 '15 at 11:21












        • Good answer, just a typo. It should be hexValue.substring. docs.oracle.com/javase/7/docs/api/java/lang/…
          – Paul
          Nov 15 '18 at 1:02














        • 1




          plus voted,Your answer actually teaches how to catch fish
          – HRgiger
          Oct 14 '15 at 11:21












        • Good answer, just a typo. It should be hexValue.substring. docs.oracle.com/javase/7/docs/api/java/lang/…
          – Paul
          Nov 15 '18 at 1:02








        1




        1




        plus voted,Your answer actually teaches how to catch fish
        – HRgiger
        Oct 14 '15 at 11:21






        plus voted,Your answer actually teaches how to catch fish
        – HRgiger
        Oct 14 '15 at 11:21














        Good answer, just a typo. It should be hexValue.substring. docs.oracle.com/javase/7/docs/api/java/lang/…
        – Paul
        Nov 15 '18 at 1:02




        Good answer, just a typo. It should be hexValue.substring. docs.oracle.com/javase/7/docs/api/java/lang/…
        – Paul
        Nov 15 '18 at 1:02











        0














        public String convertHexToString(String hex){

        StringBuilder sb = new StringBuilder();
        StringBuilder temp = new StringBuilder();

        for( int i=0; i<hex.length()-1; i+=2 ){


        String output = hex.substring(i, (i + 2));

        int decimal = Integer.parseInt(output, 16);

        sb.append((char)decimal);

        temp.append(decimal);
        temp.append(".");
        }
        System.out.println("Decimal : " + temp.toString());

        return sb.toString();


        }






        share|improve this answer


























          0














          public String convertHexToString(String hex){

          StringBuilder sb = new StringBuilder();
          StringBuilder temp = new StringBuilder();

          for( int i=0; i<hex.length()-1; i+=2 ){


          String output = hex.substring(i, (i + 2));

          int decimal = Integer.parseInt(output, 16);

          sb.append((char)decimal);

          temp.append(decimal);
          temp.append(".");
          }
          System.out.println("Decimal : " + temp.toString());

          return sb.toString();


          }






          share|improve this answer
























            0












            0








            0






            public String convertHexToString(String hex){

            StringBuilder sb = new StringBuilder();
            StringBuilder temp = new StringBuilder();

            for( int i=0; i<hex.length()-1; i+=2 ){


            String output = hex.substring(i, (i + 2));

            int decimal = Integer.parseInt(output, 16);

            sb.append((char)decimal);

            temp.append(decimal);
            temp.append(".");
            }
            System.out.println("Decimal : " + temp.toString());

            return sb.toString();


            }






            share|improve this answer












            public String convertHexToString(String hex){

            StringBuilder sb = new StringBuilder();
            StringBuilder temp = new StringBuilder();

            for( int i=0; i<hex.length()-1; i+=2 ){


            String output = hex.substring(i, (i + 2));

            int decimal = Integer.parseInt(output, 16);

            sb.append((char)decimal);

            temp.append(decimal);
            temp.append(".");
            }
            System.out.println("Decimal : " + temp.toString());

            return sb.toString();


            }







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jul 5 '13 at 13:05









            Devi Kiran

            4281416




            4281416























                0














                You can use the following method:



                public static String convertHexToIP(String hex)
                {
                String ip= "";

                for (int j = 0; j < hex.length(); j+=2) {
                String sub = hex.substring(j, j+2);
                int num = Integer.parseInt(sub, 16);
                ip += num+".";
                }

                ip = ip.substring(0, ip.length()-1);
                return ip;
                }





                share|improve this answer


























                  0














                  You can use the following method:



                  public static String convertHexToIP(String hex)
                  {
                  String ip= "";

                  for (int j = 0; j < hex.length(); j+=2) {
                  String sub = hex.substring(j, j+2);
                  int num = Integer.parseInt(sub, 16);
                  ip += num+".";
                  }

                  ip = ip.substring(0, ip.length()-1);
                  return ip;
                  }





                  share|improve this answer
























                    0












                    0








                    0






                    You can use the following method:



                    public static String convertHexToIP(String hex)
                    {
                    String ip= "";

                    for (int j = 0; j < hex.length(); j+=2) {
                    String sub = hex.substring(j, j+2);
                    int num = Integer.parseInt(sub, 16);
                    ip += num+".";
                    }

                    ip = ip.substring(0, ip.length()-1);
                    return ip;
                    }





                    share|improve this answer












                    You can use the following method:



                    public static String convertHexToIP(String hex)
                    {
                    String ip= "";

                    for (int j = 0; j < hex.length(); j+=2) {
                    String sub = hex.substring(j, j+2);
                    int num = Integer.parseInt(sub, 16);
                    ip += num+".";
                    }

                    ip = ip.substring(0, ip.length()-1);
                    return ip;
                    }






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Jul 5 '13 at 13:09









                    Rahul Bobhate

                    3,38921442




                    3,38921442























                        0














                        The accepted answer has a requirement that, the hex must be even-length.
                        Here is my answer:



                        private String getIpByHex(String hex) {
                        Long ipLong = Long.parseLong(hex, 16);
                        String ipString = String.format("%d.%d.%d.%d", ipLong >> 24,
                        ipLong >> 16 & 0x00000000000000FF,
                        ipLong >> 8 & 0x00000000000000FF,
                        ipLong & 0x00000000000000FF);

                        return ipString;
                        }





                        share|improve this answer


























                          0














                          The accepted answer has a requirement that, the hex must be even-length.
                          Here is my answer:



                          private String getIpByHex(String hex) {
                          Long ipLong = Long.parseLong(hex, 16);
                          String ipString = String.format("%d.%d.%d.%d", ipLong >> 24,
                          ipLong >> 16 & 0x00000000000000FF,
                          ipLong >> 8 & 0x00000000000000FF,
                          ipLong & 0x00000000000000FF);

                          return ipString;
                          }





                          share|improve this answer
























                            0












                            0








                            0






                            The accepted answer has a requirement that, the hex must be even-length.
                            Here is my answer:



                            private String getIpByHex(String hex) {
                            Long ipLong = Long.parseLong(hex, 16);
                            String ipString = String.format("%d.%d.%d.%d", ipLong >> 24,
                            ipLong >> 16 & 0x00000000000000FF,
                            ipLong >> 8 & 0x00000000000000FF,
                            ipLong & 0x00000000000000FF);

                            return ipString;
                            }





                            share|improve this answer












                            The accepted answer has a requirement that, the hex must be even-length.
                            Here is my answer:



                            private String getIpByHex(String hex) {
                            Long ipLong = Long.parseLong(hex, 16);
                            String ipString = String.format("%d.%d.%d.%d", ipLong >> 24,
                            ipLong >> 16 & 0x00000000000000FF,
                            ipLong >> 8 & 0x00000000000000FF,
                            ipLong & 0x00000000000000FF);

                            return ipString;
                            }






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 21 '18 at 9:53









                            Ryder

                            64




                            64























                                -1














                                You can split it 2 characters, and then use Integer.parse(string, radix) to convert to integer values



                                http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Integer.html#parseInt(java.lang.String,int)






                                share|improve this answer





















                                • The link is dead.
                                  – user2796515
                                  Oct 10 '18 at 15:02
















                                -1














                                You can split it 2 characters, and then use Integer.parse(string, radix) to convert to integer values



                                http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Integer.html#parseInt(java.lang.String,int)






                                share|improve this answer





















                                • The link is dead.
                                  – user2796515
                                  Oct 10 '18 at 15:02














                                -1












                                -1








                                -1






                                You can split it 2 characters, and then use Integer.parse(string, radix) to convert to integer values



                                http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Integer.html#parseInt(java.lang.String,int)






                                share|improve this answer












                                You can split it 2 characters, and then use Integer.parse(string, radix) to convert to integer values



                                http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Integer.html#parseInt(java.lang.String,int)







                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered Jul 5 '13 at 12:55









                                The Tosters

                                132111




                                132111












                                • The link is dead.
                                  – user2796515
                                  Oct 10 '18 at 15:02


















                                • The link is dead.
                                  – user2796515
                                  Oct 10 '18 at 15:02
















                                The link is dead.
                                – user2796515
                                Oct 10 '18 at 15:02




                                The link is dead.
                                – user2796515
                                Oct 10 '18 at 15:02


















                                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.





                                Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                Please pay close attention to the following guidance:


                                • 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%2f17489404%2fconvert-hexadecimal-string-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







                                Popular posts from this blog

                                404 Error Contact Form 7 ajax form submitting

                                How to know if a Active Directory user can login interactively

                                Refactoring coordinates for Minecraft Pi buildings written in Python