android学习笔记

String转换为Int类型的时候报错NumberFormatE

2020-06-30  本文已影响0人  鼾声鼾语
 java.lang.NumberFormatException: Invalid int: "18DA10FA"

this.iso15765 = new ISO15765(Integer.parseInt(request_can_id,16), Integer.parseInt(response_can_id,16));

18DA10FA转换为Int类型的时候,要添加参数16,否则会报错

1,正确写法:

        String str="A5";
        int bb = 0;
        try{
              bb=Integer.valueOf(str,16).intValue();
        }catch(NumberFormatException ee){
 ee.printStackTrace();
        }
        
        System.out.print(bb);
打印:165

2,错误写法:

String str="A5";
        int bb = 0;
        try{
              bb=Integer.valueOf(str).intValue();
        }catch(NumberFormatException ee){
 ee.printStackTrace();
        }
        
        System.out.print(bb);

打印:

java.lang.NumberFormatException: For input string: "A5"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:580)
    at java.lang.Integer.valueOf(Integer.java:766)
    at Test.main(Test.java:16)
上一篇 下一篇

猜你喜欢

热点阅读