NumberFormatException - with String of an Hex Value












0















I got a stream of byte this stream is a bytearray ( byte stream ) now i want parse this byte first to an hex value, e.g. 80 = "0x50" then transform this to an int



But for some values like 0xB8 i got an `java.lang.NumberFormatException: For input string: "0xB8"



How could i manage this exception? Maybe with an other type of parse or different type data?



byte k = stream[i];

int b = 0;
try {
b = Integer.parseInt(String.format("0x%02X",k),16);
} catch (NumberFormatException e) {
e.printStackTrace();
}









share|improve this question

























  • Check this stackoverflow.com/questions/11377944/…

    – mettleap
    Nov 15 '18 at 17:31
















0















I got a stream of byte this stream is a bytearray ( byte stream ) now i want parse this byte first to an hex value, e.g. 80 = "0x50" then transform this to an int



But for some values like 0xB8 i got an `java.lang.NumberFormatException: For input string: "0xB8"



How could i manage this exception? Maybe with an other type of parse or different type data?



byte k = stream[i];

int b = 0;
try {
b = Integer.parseInt(String.format("0x%02X",k),16);
} catch (NumberFormatException e) {
e.printStackTrace();
}









share|improve this question

























  • Check this stackoverflow.com/questions/11377944/…

    – mettleap
    Nov 15 '18 at 17:31














0












0








0








I got a stream of byte this stream is a bytearray ( byte stream ) now i want parse this byte first to an hex value, e.g. 80 = "0x50" then transform this to an int



But for some values like 0xB8 i got an `java.lang.NumberFormatException: For input string: "0xB8"



How could i manage this exception? Maybe with an other type of parse or different type data?



byte k = stream[i];

int b = 0;
try {
b = Integer.parseInt(String.format("0x%02X",k),16);
} catch (NumberFormatException e) {
e.printStackTrace();
}









share|improve this question
















I got a stream of byte this stream is a bytearray ( byte stream ) now i want parse this byte first to an hex value, e.g. 80 = "0x50" then transform this to an int



But for some values like 0xB8 i got an `java.lang.NumberFormatException: For input string: "0xB8"



How could i manage this exception? Maybe with an other type of parse or different type data?



byte k = stream[i];

int b = 0;
try {
b = Integer.parseInt(String.format("0x%02X",k),16);
} catch (NumberFormatException e) {
e.printStackTrace();
}






java exception






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 7:45







Gianluca Benucci

















asked Nov 15 '18 at 17:21









Gianluca BenucciGianluca Benucci

779




779













  • Check this stackoverflow.com/questions/11377944/…

    – mettleap
    Nov 15 '18 at 17:31



















  • Check this stackoverflow.com/questions/11377944/…

    – mettleap
    Nov 15 '18 at 17:31

















Check this stackoverflow.com/questions/11377944/…

– mettleap
Nov 15 '18 at 17:31





Check this stackoverflow.com/questions/11377944/…

– mettleap
Nov 15 '18 at 17:31












3 Answers
3






active

oldest

votes


















2














You must use overloaded parseInt method with radix.
For hex value an example:



Integer.parseInt("0x84B",16);





share|improve this answer
























  • Always get same error. I've updated my code.

    – Gianluca Benucci
    Nov 16 '18 at 7:45











  • @GianlucaBenucci Check what it returns String.format("0x%02X",k), it can return a non-hex string.

    – Tix
    Nov 16 '18 at 7:58








  • 1





    You'll need to remove the leading 0x for this to work. Integer.parseInt("84B",16) returns 2123, but with 0x84B you get a NumberFormatException.

    – jsheeran
    Nov 16 '18 at 8:03





















0














Try this:



Integer.parseInt(String.format("0b%02X",k),16);


Binary values are written using "0bxx" syntax






share|improve this answer































    0














    I've resolved my problem with this question



    byte k = stream[i];

    int b = 0;
    try {
    String hexStringNumber = String.format("0x%02X",k);
    b = Integer.decode(hexStringNumber);
    } catch (NumberFormatException e) {
    e.printStackTrace();
    }





    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%2f53324825%2fnumberformatexception-with-string-of-an-hex-value%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      2














      You must use overloaded parseInt method with radix.
      For hex value an example:



      Integer.parseInt("0x84B",16);





      share|improve this answer
























      • Always get same error. I've updated my code.

        – Gianluca Benucci
        Nov 16 '18 at 7:45











      • @GianlucaBenucci Check what it returns String.format("0x%02X",k), it can return a non-hex string.

        – Tix
        Nov 16 '18 at 7:58








      • 1





        You'll need to remove the leading 0x for this to work. Integer.parseInt("84B",16) returns 2123, but with 0x84B you get a NumberFormatException.

        – jsheeran
        Nov 16 '18 at 8:03


















      2














      You must use overloaded parseInt method with radix.
      For hex value an example:



      Integer.parseInt("0x84B",16);





      share|improve this answer
























      • Always get same error. I've updated my code.

        – Gianluca Benucci
        Nov 16 '18 at 7:45











      • @GianlucaBenucci Check what it returns String.format("0x%02X",k), it can return a non-hex string.

        – Tix
        Nov 16 '18 at 7:58








      • 1





        You'll need to remove the leading 0x for this to work. Integer.parseInt("84B",16) returns 2123, but with 0x84B you get a NumberFormatException.

        – jsheeran
        Nov 16 '18 at 8:03
















      2












      2








      2







      You must use overloaded parseInt method with radix.
      For hex value an example:



      Integer.parseInt("0x84B",16);





      share|improve this answer













      You must use overloaded parseInt method with radix.
      For hex value an example:



      Integer.parseInt("0x84B",16);






      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Nov 15 '18 at 17:42









      TixTix

      616




      616













      • Always get same error. I've updated my code.

        – Gianluca Benucci
        Nov 16 '18 at 7:45











      • @GianlucaBenucci Check what it returns String.format("0x%02X",k), it can return a non-hex string.

        – Tix
        Nov 16 '18 at 7:58








      • 1





        You'll need to remove the leading 0x for this to work. Integer.parseInt("84B",16) returns 2123, but with 0x84B you get a NumberFormatException.

        – jsheeran
        Nov 16 '18 at 8:03





















      • Always get same error. I've updated my code.

        – Gianluca Benucci
        Nov 16 '18 at 7:45











      • @GianlucaBenucci Check what it returns String.format("0x%02X",k), it can return a non-hex string.

        – Tix
        Nov 16 '18 at 7:58








      • 1





        You'll need to remove the leading 0x for this to work. Integer.parseInt("84B",16) returns 2123, but with 0x84B you get a NumberFormatException.

        – jsheeran
        Nov 16 '18 at 8:03



















      Always get same error. I've updated my code.

      – Gianluca Benucci
      Nov 16 '18 at 7:45





      Always get same error. I've updated my code.

      – Gianluca Benucci
      Nov 16 '18 at 7:45













      @GianlucaBenucci Check what it returns String.format("0x%02X",k), it can return a non-hex string.

      – Tix
      Nov 16 '18 at 7:58







      @GianlucaBenucci Check what it returns String.format("0x%02X",k), it can return a non-hex string.

      – Tix
      Nov 16 '18 at 7:58






      1




      1





      You'll need to remove the leading 0x for this to work. Integer.parseInt("84B",16) returns 2123, but with 0x84B you get a NumberFormatException.

      – jsheeran
      Nov 16 '18 at 8:03







      You'll need to remove the leading 0x for this to work. Integer.parseInt("84B",16) returns 2123, but with 0x84B you get a NumberFormatException.

      – jsheeran
      Nov 16 '18 at 8:03















      0














      Try this:



      Integer.parseInt(String.format("0b%02X",k),16);


      Binary values are written using "0bxx" syntax






      share|improve this answer




























        0














        Try this:



        Integer.parseInt(String.format("0b%02X",k),16);


        Binary values are written using "0bxx" syntax






        share|improve this answer


























          0












          0








          0







          Try this:



          Integer.parseInt(String.format("0b%02X",k),16);


          Binary values are written using "0bxx" syntax






          share|improve this answer













          Try this:



          Integer.parseInt(String.format("0b%02X",k),16);


          Binary values are written using "0bxx" syntax







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 16 '18 at 7:59









          NickNick

          5118




          5118























              0














              I've resolved my problem with this question



              byte k = stream[i];

              int b = 0;
              try {
              String hexStringNumber = String.format("0x%02X",k);
              b = Integer.decode(hexStringNumber);
              } catch (NumberFormatException e) {
              e.printStackTrace();
              }





              share|improve this answer




























                0














                I've resolved my problem with this question



                byte k = stream[i];

                int b = 0;
                try {
                String hexStringNumber = String.format("0x%02X",k);
                b = Integer.decode(hexStringNumber);
                } catch (NumberFormatException e) {
                e.printStackTrace();
                }





                share|improve this answer


























                  0












                  0








                  0







                  I've resolved my problem with this question



                  byte k = stream[i];

                  int b = 0;
                  try {
                  String hexStringNumber = String.format("0x%02X",k);
                  b = Integer.decode(hexStringNumber);
                  } catch (NumberFormatException e) {
                  e.printStackTrace();
                  }





                  share|improve this answer













                  I've resolved my problem with this question



                  byte k = stream[i];

                  int b = 0;
                  try {
                  String hexStringNumber = String.format("0x%02X",k);
                  b = Integer.decode(hexStringNumber);
                  } catch (NumberFormatException e) {
                  e.printStackTrace();
                  }






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 16 '18 at 8:06









                  Gianluca BenucciGianluca Benucci

                  779




                  779






























                      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%2f53324825%2fnumberformatexception-with-string-of-an-hex-value%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

                      Florida Star v. B. J. F.

                      Danny Elfman

                      Lugert, Oklahoma