0

I receive through the serial an ascii value used to display the rpm value of an encoder, I am trying to convert these values ​​to an int but I am having a problem with some values, the following error returns me sometimes java.lang.NumberFormatException: Invalid int: "2"

 public void run() {
                    byte[] rpm =  (read.getBytes(StandardCharsets.US_ASCII));


                    if(rpm.length >=12) {


                       char ch0 = (char) rpm[9];
                        char ch1 = (char) rpm[10];


                        String s = new StringBuilder().append(ch0).append(ch1).toString();

                         a = Integer.parseInt(s,16);

the log for values ch0 and ch1

enter image description here

  • 1
    Looks like its returning Hexadecimal values, if possible please provide some examples of the values being returned by the encoder. If so check this solution: https://stackoverflow.com/questions/11194513/convert-hex-string-to-int – PSM Aug 12 '20 at 14:03
  • @PSM I change the code to a= Integer.parseInt(s,16) and the error persist but now java.lang.NumberFormatException: Invalid int: "2" – Paulo Roberto Lima Aug 12 '20 at 14:16
  • 1
    Paulo, due to your edit, your question no longer makes sense. The error you get (invalid int 0D) is no longer what you get with this code, now that you added the 16. – rzwitserloot Aug 12 '20 at 14:24

1 Answers1

1

The error 'invalid int: "2"' cannot occur here. It's actually invalid int: "X2X", where one of the Xs is some space-looking character, probably an actual space (ascii 0x20), possibly a null character (ascii 0x00).

You'd need to read the docs of the protocol. evidently it sometimes sends 0x30 0x44 (which is ascii-ese for "0D"), which is to be parsed as a hex string and meaning: the decimal value of 13. But sometimes it sends, for example, 0x20 0x32 (ascii-ese for a space followed by the digit 2), which you can't pass to parseInt like this, you'd have to get rid of that space.

Your edits and comments don't actually make clear what you get; print the raw value of rpm[9] and rpm[10] to know.

Another option is that you've desynced: The protocol for example says that the 'next 4 bytes are the length' and you only read 3, so now you're off-by 1, and you're reading a space-like character which is not part of the hex digit but the earlier stuff, and the first of the 2 hex digits (2), and then tossing that at parseInt. I say that, because a protocol that sometimes sends "0D" and sometimes sends " 2" (that's a space, then a 2), is bizarre. It feels like the more logical explanation is that your code has a bug.

rzwitserloot
  • 85,357
  • 5
  • 51
  • 72
  • I'm sorry for the confused question, but thank you very much helped me the problem that was occurring was that for some reason the data packet I received was sometimes smaller than expected and the byte read by the rpm [10] was considered to be [ 0x20] that generated the error in Parseint – Paulo Roberto Lima Aug 12 '20 at 17:07