Convert a String of Hex into ASCII in Java





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







31















I hope this isn't too much of a stupid question, I have looked on 5 different pages of Google results but haven't been able to find anything on this.



What I need to do is convert a string that contains all Hex characters into ASCII for example



String fileName = 

75546f7272656e745c436f6d706c657465645c6e667375635f6f73745f62795f6d757374616e675c50656e64756c756d2d392c303030204d696c65732e6d7033006d7033006d7033004472756d202620426173730050656e64756c756d00496e2053696c69636f00496e2053696c69636f2a3b2a0050656e64756c756d0050656e64756c756d496e2053696c69636f303038004472756d2026204261737350656e64756c756d496e2053696c69636f30303800392c303030204d696c6573203c4d757374616e673e50656e64756c756d496e2053696c69636f3030380050656e64756c756d50656e64756c756d496e2053696c69636f303038004d50330000


Every way I have seen makes it seems like you have to put it into an array first. Is there no way to loop through each two and convert them?










share|improve this question































    31















    I hope this isn't too much of a stupid question, I have looked on 5 different pages of Google results but haven't been able to find anything on this.



    What I need to do is convert a string that contains all Hex characters into ASCII for example



    String fileName = 

    75546f7272656e745c436f6d706c657465645c6e667375635f6f73745f62795f6d757374616e675c50656e64756c756d2d392c303030204d696c65732e6d7033006d7033006d7033004472756d202620426173730050656e64756c756d00496e2053696c69636f00496e2053696c69636f2a3b2a0050656e64756c756d0050656e64756c756d496e2053696c69636f303038004472756d2026204261737350656e64756c756d496e2053696c69636f30303800392c303030204d696c6573203c4d757374616e673e50656e64756c756d496e2053696c69636f3030380050656e64756c756d50656e64756c756d496e2053696c69636f303038004d50330000


    Every way I have seen makes it seems like you have to put it into an array first. Is there no way to loop through each two and convert them?










    share|improve this question



























      31












      31








      31


      11






      I hope this isn't too much of a stupid question, I have looked on 5 different pages of Google results but haven't been able to find anything on this.



      What I need to do is convert a string that contains all Hex characters into ASCII for example



      String fileName = 

      75546f7272656e745c436f6d706c657465645c6e667375635f6f73745f62795f6d757374616e675c50656e64756c756d2d392c303030204d696c65732e6d7033006d7033006d7033004472756d202620426173730050656e64756c756d00496e2053696c69636f00496e2053696c69636f2a3b2a0050656e64756c756d0050656e64756c756d496e2053696c69636f303038004472756d2026204261737350656e64756c756d496e2053696c69636f30303800392c303030204d696c6573203c4d757374616e673e50656e64756c756d496e2053696c69636f3030380050656e64756c756d50656e64756c756d496e2053696c69636f303038004d50330000


      Every way I have seen makes it seems like you have to put it into an array first. Is there no way to loop through each two and convert them?










      share|improve this question
















      I hope this isn't too much of a stupid question, I have looked on 5 different pages of Google results but haven't been able to find anything on this.



      What I need to do is convert a string that contains all Hex characters into ASCII for example



      String fileName = 

      75546f7272656e745c436f6d706c657465645c6e667375635f6f73745f62795f6d757374616e675c50656e64756c756d2d392c303030204d696c65732e6d7033006d7033006d7033004472756d202620426173730050656e64756c756d00496e2053696c69636f00496e2053696c69636f2a3b2a0050656e64756c756d0050656e64756c756d496e2053696c69636f303038004472756d2026204261737350656e64756c756d496e2053696c69636f30303800392c303030204d696c6573203c4d757374616e673e50656e64756c756d496e2053696c69636f3030380050656e64756c756d50656e64756c756d496e2053696c69636f303038004d50330000


      Every way I have seen makes it seems like you have to put it into an array first. Is there no way to loop through each two and convert them?







      java string ascii hex






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 25 '15 at 4:07









      Brayden

      476




      476










      asked Jan 24 '11 at 18:30









      JamesJames

      178136




      178136
























          7 Answers
          7






          active

          oldest

          votes


















          92














          Just use a for loop to go through each couple of characters in the string, convert them to a character and then whack the character on the end of a string builder:



          public static void main(String args) {
          String hex = "75546f7272656e745c436f6d706c657465645c6e667375635f6f73745f62795f6d757374616e675c50656e64756c756d2d392c303030204d696c65732e6d7033006d7033006d7033004472756d202620426173730050656e64756c756d00496e2053696c69636f00496e2053696c69636f2a3b2a0050656e64756c756d0050656e64756c756d496e2053696c69636f303038004472756d2026204261737350656e64756c756d496e2053696c69636f30303800392c303030204d696c6573203c4d757374616e673e50656e64756c756d496e2053696c69636f3030380050656e64756c756d50656e64756c756d496e2053696c69636f303038004d50330000";
          StringBuilder output = new StringBuilder();
          for (int i = 0; i < hex.length(); i+=2) {
          String str = hex.substring(i, i+2);
          output.append((char)Integer.parseInt(str, 16));
          }
          System.out.println(output);
          }


          This gives a few lines starting with the following:




          uTorrentCompletednfsuc_ost_by_mustangPendulum-9,000 Miles.mp3




          Hmmm... :-)






          share|improve this answer





















          • 87





            This question wins the award of "Most Self-Incriminating Question of the Year".

            – Cory Klein
            Jan 24 '11 at 18:46






          • 3





            Ha ha good point, but lucky for me this file is from a once live case so I'm in the clear but I didn't think ha ha! Made me laugh though :)

            – James
            Jan 24 '11 at 18:54






          • 1





            Thank you "berry120" that has helped me loads! :)

            – James
            Jan 24 '11 at 18:56











          • Given that the size of the string is known in advance, one might optimise the StringBuilder to hold it all or even use just a char. Only relevant if this will be run often, of course.

            – entonio
            Feb 22 '13 at 16:16






          • 5





            I know this is an old question but just for info Guava 14 introduced a BaseEncoding class which would be used like new String(BaseEncoding.base16().lowerCase().decode(hex), Charsets.US_ASCII) - the BaseEncoding instance can be cached as it's immutable

            – Matt
            Mar 22 '13 at 11:27





















          6














          Easiest way to do it with javax.xml.bind.DatatypeConverter:



              String hex = "75546f7272656e745c436f6d706c657465645c6e667375635f6f73745f62795f6d757374616e675c50656e64756c756d2d392c303030204d696c65732e6d7033006d7033006d7033004472756d202620426173730050656e64756c756d00496e2053696c69636f00496e2053696c69636f2a3b2a0050656e64756c756d0050656e64756c756d496e2053696c69636f303038004472756d2026204261737350656e64756c756d496e2053696c69636f30303800392c303030204d696c6573203c4d757374616e673e50656e64756c756d496e2053696c69636f3030380050656e64756c756d50656e64756c756d496e2053696c69636f303038004d50330000";
          byte s = DatatypeConverter.parseHexBinary(hex);
          System.out.println(new String(s));





          share|improve this answer



















          • 1





            This solution gives the same results as the accepted answer.

            – LeHill
            Sep 8 '16 at 20:34











          • in my case the string started with 0x and I got an exception that complained about illegal characters (the x in this case) so I just had to do a substring(2) to exclude those. ^_^

            – Ted Delezene
            Sep 24 '18 at 22:44



















          2














          Check out Convert a string representation of a hex dump to a byte array using Java?



          Disregarding encoding, etc. you can do new String (hexStringToByteArray("75546..."));






          share|improve this answer

































            2














            So as I understand it, you need to pull out successive pairs of hex digits, then decode that 2-digit hex number and take the corresponding char:



            String s = "...";
            StringBuilder sb = new StringBuilder(s.length() / 2);
            for (int i = 0; i < s.length(); i+=2) {
            String hex = "" + s.charAt(i) + s.charAt(i+1);
            int ival = Integer.parseInt(hex, 16);
            sb.append((char) ival);
            }
            String string = sb.toString();





            share|improve this answer































              2














              String hexToAscii(String s) {
              int n = s.length();
              StringBuilder sb = new StringBuilder(n / 2);
              for (int i = 0; i < n; i += 2) {
              char a = s.charAt(i);
              char b = s.charAt(i + 1);
              sb.append((char) ((hexToInt(a) << 4) | hexToInt(b)));
              }
              return sb.toString();
              }

              private static int hexToInt(char ch) {
              if ('a' <= ch && ch <= 'f') { return ch - 'a' + 10; }
              if ('A' <= ch && ch <= 'F') { return ch - 'A' + 10; }
              if ('0' <= ch && ch <= '9') { return ch - '0'; }
              throw new IllegalArgumentException(String.valueOf(ch));
              }





              share|improve this answer































                1














                //%%%%%%%%%%%%%%%%%%%%%% HEX to ASCII %%%%%%%%%%%%%%%%%%%%%%
                public String convertHexToString(String hex){

                String ascii="";
                String str;

                // Convert hex string to "even" length
                int rmd,length;
                length=hex.length();
                rmd =length % 2;
                if(rmd==1)
                hex = "0"+hex;

                // split into two characters
                for( int i=0; i<hex.length()-1; i+=2 ){

                //split the hex into pairs
                String pair = hex.substring(i, (i + 2));
                //convert hex to decimal
                int dec = Integer.parseInt(pair, 16);
                str=CheckCode(dec);
                ascii=ascii+" "+str;
                }
                return ascii;
                }

                public String CheckCode(int dec){
                String str;

                //convert the decimal to character
                str = Character.toString((char) dec);

                if(dec<32 || dec>126 && dec<161)
                str="n/a";
                return str;
                }





                share|improve this answer

































                  0














                  To this case, I have a hexadecimal data format into an int array and I want to convert them on String.



                  int encodeHex = new int { 0x48, 0x65, 0x6c, 0x6c, 0x6f }; // Hello encode
                  for (int i = 0; i < encodeHex.length; i++) {
                  System.out.print((char) (encodeHex[i]));
                  }





                  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%2f4785654%2fconvert-a-string-of-hex-into-ascii-in-java%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown

























                    7 Answers
                    7






                    active

                    oldest

                    votes








                    7 Answers
                    7






                    active

                    oldest

                    votes









                    active

                    oldest

                    votes






                    active

                    oldest

                    votes









                    92














                    Just use a for loop to go through each couple of characters in the string, convert them to a character and then whack the character on the end of a string builder:



                    public static void main(String args) {
                    String hex = "75546f7272656e745c436f6d706c657465645c6e667375635f6f73745f62795f6d757374616e675c50656e64756c756d2d392c303030204d696c65732e6d7033006d7033006d7033004472756d202620426173730050656e64756c756d00496e2053696c69636f00496e2053696c69636f2a3b2a0050656e64756c756d0050656e64756c756d496e2053696c69636f303038004472756d2026204261737350656e64756c756d496e2053696c69636f30303800392c303030204d696c6573203c4d757374616e673e50656e64756c756d496e2053696c69636f3030380050656e64756c756d50656e64756c756d496e2053696c69636f303038004d50330000";
                    StringBuilder output = new StringBuilder();
                    for (int i = 0; i < hex.length(); i+=2) {
                    String str = hex.substring(i, i+2);
                    output.append((char)Integer.parseInt(str, 16));
                    }
                    System.out.println(output);
                    }


                    This gives a few lines starting with the following:




                    uTorrentCompletednfsuc_ost_by_mustangPendulum-9,000 Miles.mp3




                    Hmmm... :-)






                    share|improve this answer





















                    • 87





                      This question wins the award of "Most Self-Incriminating Question of the Year".

                      – Cory Klein
                      Jan 24 '11 at 18:46






                    • 3





                      Ha ha good point, but lucky for me this file is from a once live case so I'm in the clear but I didn't think ha ha! Made me laugh though :)

                      – James
                      Jan 24 '11 at 18:54






                    • 1





                      Thank you "berry120" that has helped me loads! :)

                      – James
                      Jan 24 '11 at 18:56











                    • Given that the size of the string is known in advance, one might optimise the StringBuilder to hold it all or even use just a char. Only relevant if this will be run often, of course.

                      – entonio
                      Feb 22 '13 at 16:16






                    • 5





                      I know this is an old question but just for info Guava 14 introduced a BaseEncoding class which would be used like new String(BaseEncoding.base16().lowerCase().decode(hex), Charsets.US_ASCII) - the BaseEncoding instance can be cached as it's immutable

                      – Matt
                      Mar 22 '13 at 11:27


















                    92














                    Just use a for loop to go through each couple of characters in the string, convert them to a character and then whack the character on the end of a string builder:



                    public static void main(String args) {
                    String hex = "75546f7272656e745c436f6d706c657465645c6e667375635f6f73745f62795f6d757374616e675c50656e64756c756d2d392c303030204d696c65732e6d7033006d7033006d7033004472756d202620426173730050656e64756c756d00496e2053696c69636f00496e2053696c69636f2a3b2a0050656e64756c756d0050656e64756c756d496e2053696c69636f303038004472756d2026204261737350656e64756c756d496e2053696c69636f30303800392c303030204d696c6573203c4d757374616e673e50656e64756c756d496e2053696c69636f3030380050656e64756c756d50656e64756c756d496e2053696c69636f303038004d50330000";
                    StringBuilder output = new StringBuilder();
                    for (int i = 0; i < hex.length(); i+=2) {
                    String str = hex.substring(i, i+2);
                    output.append((char)Integer.parseInt(str, 16));
                    }
                    System.out.println(output);
                    }


                    This gives a few lines starting with the following:




                    uTorrentCompletednfsuc_ost_by_mustangPendulum-9,000 Miles.mp3




                    Hmmm... :-)






                    share|improve this answer





















                    • 87





                      This question wins the award of "Most Self-Incriminating Question of the Year".

                      – Cory Klein
                      Jan 24 '11 at 18:46






                    • 3





                      Ha ha good point, but lucky for me this file is from a once live case so I'm in the clear but I didn't think ha ha! Made me laugh though :)

                      – James
                      Jan 24 '11 at 18:54






                    • 1





                      Thank you "berry120" that has helped me loads! :)

                      – James
                      Jan 24 '11 at 18:56











                    • Given that the size of the string is known in advance, one might optimise the StringBuilder to hold it all or even use just a char. Only relevant if this will be run often, of course.

                      – entonio
                      Feb 22 '13 at 16:16






                    • 5





                      I know this is an old question but just for info Guava 14 introduced a BaseEncoding class which would be used like new String(BaseEncoding.base16().lowerCase().decode(hex), Charsets.US_ASCII) - the BaseEncoding instance can be cached as it's immutable

                      – Matt
                      Mar 22 '13 at 11:27
















                    92












                    92








                    92







                    Just use a for loop to go through each couple of characters in the string, convert them to a character and then whack the character on the end of a string builder:



                    public static void main(String args) {
                    String hex = "75546f7272656e745c436f6d706c657465645c6e667375635f6f73745f62795f6d757374616e675c50656e64756c756d2d392c303030204d696c65732e6d7033006d7033006d7033004472756d202620426173730050656e64756c756d00496e2053696c69636f00496e2053696c69636f2a3b2a0050656e64756c756d0050656e64756c756d496e2053696c69636f303038004472756d2026204261737350656e64756c756d496e2053696c69636f30303800392c303030204d696c6573203c4d757374616e673e50656e64756c756d496e2053696c69636f3030380050656e64756c756d50656e64756c756d496e2053696c69636f303038004d50330000";
                    StringBuilder output = new StringBuilder();
                    for (int i = 0; i < hex.length(); i+=2) {
                    String str = hex.substring(i, i+2);
                    output.append((char)Integer.parseInt(str, 16));
                    }
                    System.out.println(output);
                    }


                    This gives a few lines starting with the following:




                    uTorrentCompletednfsuc_ost_by_mustangPendulum-9,000 Miles.mp3




                    Hmmm... :-)






                    share|improve this answer















                    Just use a for loop to go through each couple of characters in the string, convert them to a character and then whack the character on the end of a string builder:



                    public static void main(String args) {
                    String hex = "75546f7272656e745c436f6d706c657465645c6e667375635f6f73745f62795f6d757374616e675c50656e64756c756d2d392c303030204d696c65732e6d7033006d7033006d7033004472756d202620426173730050656e64756c756d00496e2053696c69636f00496e2053696c69636f2a3b2a0050656e64756c756d0050656e64756c756d496e2053696c69636f303038004472756d2026204261737350656e64756c756d496e2053696c69636f30303800392c303030204d696c6573203c4d757374616e673e50656e64756c756d496e2053696c69636f3030380050656e64756c756d50656e64756c756d496e2053696c69636f303038004d50330000";
                    StringBuilder output = new StringBuilder();
                    for (int i = 0; i < hex.length(); i+=2) {
                    String str = hex.substring(i, i+2);
                    output.append((char)Integer.parseInt(str, 16));
                    }
                    System.out.println(output);
                    }


                    This gives a few lines starting with the following:




                    uTorrentCompletednfsuc_ost_by_mustangPendulum-9,000 Miles.mp3




                    Hmmm... :-)







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited May 8 '13 at 9:32

























                    answered Jan 24 '11 at 18:40









                    Michael BerryMichael Berry

                    44.1k16115170




                    44.1k16115170








                    • 87





                      This question wins the award of "Most Self-Incriminating Question of the Year".

                      – Cory Klein
                      Jan 24 '11 at 18:46






                    • 3





                      Ha ha good point, but lucky for me this file is from a once live case so I'm in the clear but I didn't think ha ha! Made me laugh though :)

                      – James
                      Jan 24 '11 at 18:54






                    • 1





                      Thank you "berry120" that has helped me loads! :)

                      – James
                      Jan 24 '11 at 18:56











                    • Given that the size of the string is known in advance, one might optimise the StringBuilder to hold it all or even use just a char. Only relevant if this will be run often, of course.

                      – entonio
                      Feb 22 '13 at 16:16






                    • 5





                      I know this is an old question but just for info Guava 14 introduced a BaseEncoding class which would be used like new String(BaseEncoding.base16().lowerCase().decode(hex), Charsets.US_ASCII) - the BaseEncoding instance can be cached as it's immutable

                      – Matt
                      Mar 22 '13 at 11:27
















                    • 87





                      This question wins the award of "Most Self-Incriminating Question of the Year".

                      – Cory Klein
                      Jan 24 '11 at 18:46






                    • 3





                      Ha ha good point, but lucky for me this file is from a once live case so I'm in the clear but I didn't think ha ha! Made me laugh though :)

                      – James
                      Jan 24 '11 at 18:54






                    • 1





                      Thank you "berry120" that has helped me loads! :)

                      – James
                      Jan 24 '11 at 18:56











                    • Given that the size of the string is known in advance, one might optimise the StringBuilder to hold it all or even use just a char. Only relevant if this will be run often, of course.

                      – entonio
                      Feb 22 '13 at 16:16






                    • 5





                      I know this is an old question but just for info Guava 14 introduced a BaseEncoding class which would be used like new String(BaseEncoding.base16().lowerCase().decode(hex), Charsets.US_ASCII) - the BaseEncoding instance can be cached as it's immutable

                      – Matt
                      Mar 22 '13 at 11:27










                    87




                    87





                    This question wins the award of "Most Self-Incriminating Question of the Year".

                    – Cory Klein
                    Jan 24 '11 at 18:46





                    This question wins the award of "Most Self-Incriminating Question of the Year".

                    – Cory Klein
                    Jan 24 '11 at 18:46




                    3




                    3





                    Ha ha good point, but lucky for me this file is from a once live case so I'm in the clear but I didn't think ha ha! Made me laugh though :)

                    – James
                    Jan 24 '11 at 18:54





                    Ha ha good point, but lucky for me this file is from a once live case so I'm in the clear but I didn't think ha ha! Made me laugh though :)

                    – James
                    Jan 24 '11 at 18:54




                    1




                    1





                    Thank you "berry120" that has helped me loads! :)

                    – James
                    Jan 24 '11 at 18:56





                    Thank you "berry120" that has helped me loads! :)

                    – James
                    Jan 24 '11 at 18:56













                    Given that the size of the string is known in advance, one might optimise the StringBuilder to hold it all or even use just a char. Only relevant if this will be run often, of course.

                    – entonio
                    Feb 22 '13 at 16:16





                    Given that the size of the string is known in advance, one might optimise the StringBuilder to hold it all or even use just a char. Only relevant if this will be run often, of course.

                    – entonio
                    Feb 22 '13 at 16:16




                    5




                    5





                    I know this is an old question but just for info Guava 14 introduced a BaseEncoding class which would be used like new String(BaseEncoding.base16().lowerCase().decode(hex), Charsets.US_ASCII) - the BaseEncoding instance can be cached as it's immutable

                    – Matt
                    Mar 22 '13 at 11:27







                    I know this is an old question but just for info Guava 14 introduced a BaseEncoding class which would be used like new String(BaseEncoding.base16().lowerCase().decode(hex), Charsets.US_ASCII) - the BaseEncoding instance can be cached as it's immutable

                    – Matt
                    Mar 22 '13 at 11:27















                    6














                    Easiest way to do it with javax.xml.bind.DatatypeConverter:



                        String hex = "75546f7272656e745c436f6d706c657465645c6e667375635f6f73745f62795f6d757374616e675c50656e64756c756d2d392c303030204d696c65732e6d7033006d7033006d7033004472756d202620426173730050656e64756c756d00496e2053696c69636f00496e2053696c69636f2a3b2a0050656e64756c756d0050656e64756c756d496e2053696c69636f303038004472756d2026204261737350656e64756c756d496e2053696c69636f30303800392c303030204d696c6573203c4d757374616e673e50656e64756c756d496e2053696c69636f3030380050656e64756c756d50656e64756c756d496e2053696c69636f303038004d50330000";
                    byte s = DatatypeConverter.parseHexBinary(hex);
                    System.out.println(new String(s));





                    share|improve this answer



















                    • 1





                      This solution gives the same results as the accepted answer.

                      – LeHill
                      Sep 8 '16 at 20:34











                    • in my case the string started with 0x and I got an exception that complained about illegal characters (the x in this case) so I just had to do a substring(2) to exclude those. ^_^

                      – Ted Delezene
                      Sep 24 '18 at 22:44
















                    6














                    Easiest way to do it with javax.xml.bind.DatatypeConverter:



                        String hex = "75546f7272656e745c436f6d706c657465645c6e667375635f6f73745f62795f6d757374616e675c50656e64756c756d2d392c303030204d696c65732e6d7033006d7033006d7033004472756d202620426173730050656e64756c756d00496e2053696c69636f00496e2053696c69636f2a3b2a0050656e64756c756d0050656e64756c756d496e2053696c69636f303038004472756d2026204261737350656e64756c756d496e2053696c69636f30303800392c303030204d696c6573203c4d757374616e673e50656e64756c756d496e2053696c69636f3030380050656e64756c756d50656e64756c756d496e2053696c69636f303038004d50330000";
                    byte s = DatatypeConverter.parseHexBinary(hex);
                    System.out.println(new String(s));





                    share|improve this answer



















                    • 1





                      This solution gives the same results as the accepted answer.

                      – LeHill
                      Sep 8 '16 at 20:34











                    • in my case the string started with 0x and I got an exception that complained about illegal characters (the x in this case) so I just had to do a substring(2) to exclude those. ^_^

                      – Ted Delezene
                      Sep 24 '18 at 22:44














                    6












                    6








                    6







                    Easiest way to do it with javax.xml.bind.DatatypeConverter:



                        String hex = "75546f7272656e745c436f6d706c657465645c6e667375635f6f73745f62795f6d757374616e675c50656e64756c756d2d392c303030204d696c65732e6d7033006d7033006d7033004472756d202620426173730050656e64756c756d00496e2053696c69636f00496e2053696c69636f2a3b2a0050656e64756c756d0050656e64756c756d496e2053696c69636f303038004472756d2026204261737350656e64756c756d496e2053696c69636f30303800392c303030204d696c6573203c4d757374616e673e50656e64756c756d496e2053696c69636f3030380050656e64756c756d50656e64756c756d496e2053696c69636f303038004d50330000";
                    byte s = DatatypeConverter.parseHexBinary(hex);
                    System.out.println(new String(s));





                    share|improve this answer













                    Easiest way to do it with javax.xml.bind.DatatypeConverter:



                        String hex = "75546f7272656e745c436f6d706c657465645c6e667375635f6f73745f62795f6d757374616e675c50656e64756c756d2d392c303030204d696c65732e6d7033006d7033006d7033004472756d202620426173730050656e64756c756d00496e2053696c69636f00496e2053696c69636f2a3b2a0050656e64756c756d0050656e64756c756d496e2053696c69636f303038004472756d2026204261737350656e64756c756d496e2053696c69636f30303800392c303030204d696c6573203c4d757374616e673e50656e64756c756d496e2053696c69636f3030380050656e64756c756d50656e64756c756d496e2053696c69636f303038004d50330000";
                    byte s = DatatypeConverter.parseHexBinary(hex);
                    System.out.println(new String(s));






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered May 18 '15 at 8:55









                    Nikita KoksharovNikita Koksharov

                    6,2883754




                    6,2883754








                    • 1





                      This solution gives the same results as the accepted answer.

                      – LeHill
                      Sep 8 '16 at 20:34











                    • in my case the string started with 0x and I got an exception that complained about illegal characters (the x in this case) so I just had to do a substring(2) to exclude those. ^_^

                      – Ted Delezene
                      Sep 24 '18 at 22:44














                    • 1





                      This solution gives the same results as the accepted answer.

                      – LeHill
                      Sep 8 '16 at 20:34











                    • in my case the string started with 0x and I got an exception that complained about illegal characters (the x in this case) so I just had to do a substring(2) to exclude those. ^_^

                      – Ted Delezene
                      Sep 24 '18 at 22:44








                    1




                    1





                    This solution gives the same results as the accepted answer.

                    – LeHill
                    Sep 8 '16 at 20:34





                    This solution gives the same results as the accepted answer.

                    – LeHill
                    Sep 8 '16 at 20:34













                    in my case the string started with 0x and I got an exception that complained about illegal characters (the x in this case) so I just had to do a substring(2) to exclude those. ^_^

                    – Ted Delezene
                    Sep 24 '18 at 22:44





                    in my case the string started with 0x and I got an exception that complained about illegal characters (the x in this case) so I just had to do a substring(2) to exclude those. ^_^

                    – Ted Delezene
                    Sep 24 '18 at 22:44











                    2














                    Check out Convert a string representation of a hex dump to a byte array using Java?



                    Disregarding encoding, etc. you can do new String (hexStringToByteArray("75546..."));






                    share|improve this answer






























                      2














                      Check out Convert a string representation of a hex dump to a byte array using Java?



                      Disregarding encoding, etc. you can do new String (hexStringToByteArray("75546..."));






                      share|improve this answer




























                        2












                        2








                        2







                        Check out Convert a string representation of a hex dump to a byte array using Java?



                        Disregarding encoding, etc. you can do new String (hexStringToByteArray("75546..."));






                        share|improve this answer















                        Check out Convert a string representation of a hex dump to a byte array using Java?



                        Disregarding encoding, etc. you can do new String (hexStringToByteArray("75546..."));







                        share|improve this answer














                        share|improve this answer



                        share|improve this answer








                        edited May 23 '17 at 12:32









                        Community

                        11




                        11










                        answered Jan 24 '11 at 18:40









                        Andrew T FinnellAndrew T Finnell

                        12k22642




                        12k22642























                            2














                            So as I understand it, you need to pull out successive pairs of hex digits, then decode that 2-digit hex number and take the corresponding char:



                            String s = "...";
                            StringBuilder sb = new StringBuilder(s.length() / 2);
                            for (int i = 0; i < s.length(); i+=2) {
                            String hex = "" + s.charAt(i) + s.charAt(i+1);
                            int ival = Integer.parseInt(hex, 16);
                            sb.append((char) ival);
                            }
                            String string = sb.toString();





                            share|improve this answer




























                              2














                              So as I understand it, you need to pull out successive pairs of hex digits, then decode that 2-digit hex number and take the corresponding char:



                              String s = "...";
                              StringBuilder sb = new StringBuilder(s.length() / 2);
                              for (int i = 0; i < s.length(); i+=2) {
                              String hex = "" + s.charAt(i) + s.charAt(i+1);
                              int ival = Integer.parseInt(hex, 16);
                              sb.append((char) ival);
                              }
                              String string = sb.toString();





                              share|improve this answer


























                                2












                                2








                                2







                                So as I understand it, you need to pull out successive pairs of hex digits, then decode that 2-digit hex number and take the corresponding char:



                                String s = "...";
                                StringBuilder sb = new StringBuilder(s.length() / 2);
                                for (int i = 0; i < s.length(); i+=2) {
                                String hex = "" + s.charAt(i) + s.charAt(i+1);
                                int ival = Integer.parseInt(hex, 16);
                                sb.append((char) ival);
                                }
                                String string = sb.toString();





                                share|improve this answer













                                So as I understand it, you need to pull out successive pairs of hex digits, then decode that 2-digit hex number and take the corresponding char:



                                String s = "...";
                                StringBuilder sb = new StringBuilder(s.length() / 2);
                                for (int i = 0; i < s.length(); i+=2) {
                                String hex = "" + s.charAt(i) + s.charAt(i+1);
                                int ival = Integer.parseInt(hex, 16);
                                sb.append((char) ival);
                                }
                                String string = sb.toString();






                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered Jan 24 '11 at 18:41









                                Neil CoffeyNeil Coffey

                                18.7k65476




                                18.7k65476























                                    2














                                    String hexToAscii(String s) {
                                    int n = s.length();
                                    StringBuilder sb = new StringBuilder(n / 2);
                                    for (int i = 0; i < n; i += 2) {
                                    char a = s.charAt(i);
                                    char b = s.charAt(i + 1);
                                    sb.append((char) ((hexToInt(a) << 4) | hexToInt(b)));
                                    }
                                    return sb.toString();
                                    }

                                    private static int hexToInt(char ch) {
                                    if ('a' <= ch && ch <= 'f') { return ch - 'a' + 10; }
                                    if ('A' <= ch && ch <= 'F') { return ch - 'A' + 10; }
                                    if ('0' <= ch && ch <= '9') { return ch - '0'; }
                                    throw new IllegalArgumentException(String.valueOf(ch));
                                    }





                                    share|improve this answer




























                                      2














                                      String hexToAscii(String s) {
                                      int n = s.length();
                                      StringBuilder sb = new StringBuilder(n / 2);
                                      for (int i = 0; i < n; i += 2) {
                                      char a = s.charAt(i);
                                      char b = s.charAt(i + 1);
                                      sb.append((char) ((hexToInt(a) << 4) | hexToInt(b)));
                                      }
                                      return sb.toString();
                                      }

                                      private static int hexToInt(char ch) {
                                      if ('a' <= ch && ch <= 'f') { return ch - 'a' + 10; }
                                      if ('A' <= ch && ch <= 'F') { return ch - 'A' + 10; }
                                      if ('0' <= ch && ch <= '9') { return ch - '0'; }
                                      throw new IllegalArgumentException(String.valueOf(ch));
                                      }





                                      share|improve this answer


























                                        2












                                        2








                                        2







                                        String hexToAscii(String s) {
                                        int n = s.length();
                                        StringBuilder sb = new StringBuilder(n / 2);
                                        for (int i = 0; i < n; i += 2) {
                                        char a = s.charAt(i);
                                        char b = s.charAt(i + 1);
                                        sb.append((char) ((hexToInt(a) << 4) | hexToInt(b)));
                                        }
                                        return sb.toString();
                                        }

                                        private static int hexToInt(char ch) {
                                        if ('a' <= ch && ch <= 'f') { return ch - 'a' + 10; }
                                        if ('A' <= ch && ch <= 'F') { return ch - 'A' + 10; }
                                        if ('0' <= ch && ch <= '9') { return ch - '0'; }
                                        throw new IllegalArgumentException(String.valueOf(ch));
                                        }





                                        share|improve this answer













                                        String hexToAscii(String s) {
                                        int n = s.length();
                                        StringBuilder sb = new StringBuilder(n / 2);
                                        for (int i = 0; i < n; i += 2) {
                                        char a = s.charAt(i);
                                        char b = s.charAt(i + 1);
                                        sb.append((char) ((hexToInt(a) << 4) | hexToInt(b)));
                                        }
                                        return sb.toString();
                                        }

                                        private static int hexToInt(char ch) {
                                        if ('a' <= ch && ch <= 'f') { return ch - 'a' + 10; }
                                        if ('A' <= ch && ch <= 'F') { return ch - 'A' + 10; }
                                        if ('0' <= ch && ch <= '9') { return ch - '0'; }
                                        throw new IllegalArgumentException(String.valueOf(ch));
                                        }






                                        share|improve this answer












                                        share|improve this answer



                                        share|improve this answer










                                        answered Jan 24 '11 at 18:42









                                        Mike SamuelMike Samuel

                                        94.5k23174215




                                        94.5k23174215























                                            1














                                            //%%%%%%%%%%%%%%%%%%%%%% HEX to ASCII %%%%%%%%%%%%%%%%%%%%%%
                                            public String convertHexToString(String hex){

                                            String ascii="";
                                            String str;

                                            // Convert hex string to "even" length
                                            int rmd,length;
                                            length=hex.length();
                                            rmd =length % 2;
                                            if(rmd==1)
                                            hex = "0"+hex;

                                            // split into two characters
                                            for( int i=0; i<hex.length()-1; i+=2 ){

                                            //split the hex into pairs
                                            String pair = hex.substring(i, (i + 2));
                                            //convert hex to decimal
                                            int dec = Integer.parseInt(pair, 16);
                                            str=CheckCode(dec);
                                            ascii=ascii+" "+str;
                                            }
                                            return ascii;
                                            }

                                            public String CheckCode(int dec){
                                            String str;

                                            //convert the decimal to character
                                            str = Character.toString((char) dec);

                                            if(dec<32 || dec>126 && dec<161)
                                            str="n/a";
                                            return str;
                                            }





                                            share|improve this answer






























                                              1














                                              //%%%%%%%%%%%%%%%%%%%%%% HEX to ASCII %%%%%%%%%%%%%%%%%%%%%%
                                              public String convertHexToString(String hex){

                                              String ascii="";
                                              String str;

                                              // Convert hex string to "even" length
                                              int rmd,length;
                                              length=hex.length();
                                              rmd =length % 2;
                                              if(rmd==1)
                                              hex = "0"+hex;

                                              // split into two characters
                                              for( int i=0; i<hex.length()-1; i+=2 ){

                                              //split the hex into pairs
                                              String pair = hex.substring(i, (i + 2));
                                              //convert hex to decimal
                                              int dec = Integer.parseInt(pair, 16);
                                              str=CheckCode(dec);
                                              ascii=ascii+" "+str;
                                              }
                                              return ascii;
                                              }

                                              public String CheckCode(int dec){
                                              String str;

                                              //convert the decimal to character
                                              str = Character.toString((char) dec);

                                              if(dec<32 || dec>126 && dec<161)
                                              str="n/a";
                                              return str;
                                              }





                                              share|improve this answer




























                                                1












                                                1








                                                1







                                                //%%%%%%%%%%%%%%%%%%%%%% HEX to ASCII %%%%%%%%%%%%%%%%%%%%%%
                                                public String convertHexToString(String hex){

                                                String ascii="";
                                                String str;

                                                // Convert hex string to "even" length
                                                int rmd,length;
                                                length=hex.length();
                                                rmd =length % 2;
                                                if(rmd==1)
                                                hex = "0"+hex;

                                                // split into two characters
                                                for( int i=0; i<hex.length()-1; i+=2 ){

                                                //split the hex into pairs
                                                String pair = hex.substring(i, (i + 2));
                                                //convert hex to decimal
                                                int dec = Integer.parseInt(pair, 16);
                                                str=CheckCode(dec);
                                                ascii=ascii+" "+str;
                                                }
                                                return ascii;
                                                }

                                                public String CheckCode(int dec){
                                                String str;

                                                //convert the decimal to character
                                                str = Character.toString((char) dec);

                                                if(dec<32 || dec>126 && dec<161)
                                                str="n/a";
                                                return str;
                                                }





                                                share|improve this answer















                                                //%%%%%%%%%%%%%%%%%%%%%% HEX to ASCII %%%%%%%%%%%%%%%%%%%%%%
                                                public String convertHexToString(String hex){

                                                String ascii="";
                                                String str;

                                                // Convert hex string to "even" length
                                                int rmd,length;
                                                length=hex.length();
                                                rmd =length % 2;
                                                if(rmd==1)
                                                hex = "0"+hex;

                                                // split into two characters
                                                for( int i=0; i<hex.length()-1; i+=2 ){

                                                //split the hex into pairs
                                                String pair = hex.substring(i, (i + 2));
                                                //convert hex to decimal
                                                int dec = Integer.parseInt(pair, 16);
                                                str=CheckCode(dec);
                                                ascii=ascii+" "+str;
                                                }
                                                return ascii;
                                                }

                                                public String CheckCode(int dec){
                                                String str;

                                                //convert the decimal to character
                                                str = Character.toString((char) dec);

                                                if(dec<32 || dec>126 && dec<161)
                                                str="n/a";
                                                return str;
                                                }






                                                share|improve this answer














                                                share|improve this answer



                                                share|improve this answer








                                                edited Jul 14 '13 at 9:04









                                                kleopatra

                                                45.2k1676163




                                                45.2k1676163










                                                answered Jul 14 '13 at 8:46









                                                maryanmaryan

                                                211




                                                211























                                                    0














                                                    To this case, I have a hexadecimal data format into an int array and I want to convert them on String.



                                                    int encodeHex = new int { 0x48, 0x65, 0x6c, 0x6c, 0x6f }; // Hello encode
                                                    for (int i = 0; i < encodeHex.length; i++) {
                                                    System.out.print((char) (encodeHex[i]));
                                                    }





                                                    share|improve this answer




























                                                      0














                                                      To this case, I have a hexadecimal data format into an int array and I want to convert them on String.



                                                      int encodeHex = new int { 0x48, 0x65, 0x6c, 0x6c, 0x6f }; // Hello encode
                                                      for (int i = 0; i < encodeHex.length; i++) {
                                                      System.out.print((char) (encodeHex[i]));
                                                      }





                                                      share|improve this answer


























                                                        0












                                                        0








                                                        0







                                                        To this case, I have a hexadecimal data format into an int array and I want to convert them on String.



                                                        int encodeHex = new int { 0x48, 0x65, 0x6c, 0x6c, 0x6f }; // Hello encode
                                                        for (int i = 0; i < encodeHex.length; i++) {
                                                        System.out.print((char) (encodeHex[i]));
                                                        }





                                                        share|improve this answer













                                                        To this case, I have a hexadecimal data format into an int array and I want to convert them on String.



                                                        int encodeHex = new int { 0x48, 0x65, 0x6c, 0x6c, 0x6f }; // Hello encode
                                                        for (int i = 0; i < encodeHex.length; i++) {
                                                        System.out.print((char) (encodeHex[i]));
                                                        }






                                                        share|improve this answer












                                                        share|improve this answer



                                                        share|improve this answer










                                                        answered Sep 3 '15 at 15:22









                                                        Pablo TisseraPablo Tissera

                                                        11




                                                        11






























                                                            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%2f4785654%2fconvert-a-string-of-hex-into-ascii-in-java%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

                                                            Can a sorcerer learn a 5th-level spell early by creating spell slots using the Font of Magic feature?

                                                            Does disintegrating a polymorphed enemy still kill it after the 2018 errata?

                                                            A Topological Invariant for $pi_3(U(n))$