将ASCII转换为整数值

huangapple go评论131阅读模式
英文:

Convert ascii to int Value

问题

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

将ASCII转换为整数值

英文:

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

将ASCII转换为整数值

答案1

得分: 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.

英文:

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.

huangapple
  • 本文由 发表于 2020年8月12日 22:01:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/63378184.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定