-2

I need some help in converting the answers I get from my OBD adapter in my car to decimal and then later adding whatever value comes out of the conversion to a formula and printed out.

public void run() {
     OBDcmds();

     try {
         OdbRawCommand ANS = new OdbRawCommand("22 40 90");


         while (!Thread.currentThread().isInterrupted()) {
             Log.d("Log", "ANS: " + ANS.getFormattedResult());

             try {
                 ANS.run(mmInStream, mmOutStream);
             } catch (InterruptedException e) {
                 e.printStackTrace();
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("inside catch before while");
            }
        }

ANS when asked will return something like "6240901F30" where the first 6 numbers are irrelevant to the answer (6240901F30). So what we have left is the last (in this case) 4 numbers (1F30) which are in Hexadecimal and need to be converted to Decimal. This value is then later added to a formula x * 0,0625 - 512. It needs to jump over those six first numbers otherwise the answer will be wrong.

How would I be able to pull this off?

swess
  • 171
  • 19

1 Answers1

1

Use Integer.parseInt with the "radix" argument :

System.out.println(Integer.parseInt("1F30", 16));

Output :

7984

Arnaud Denoyelle
  • 29,980
  • 16
  • 92
  • 148
  • @EvgeniyMishustin also known as "base", it is the number of different digits that exist in a given numerical representation. For example, `binary <=> radix = 2`; `octal <=> radix = 8`; `hexadecimal <=> radix = 16`. – Arnaud Denoyelle May 16 '16 at 12:04
  • Altough "1F30" in not a static value. Meaning that "1F30" will update to a different number since I have it in a while-loop. This is calculating the current of the battery in the car, therefore this will not work sadly. – swess May 16 '16 at 12:05
  • @swess You can replace the constant with any String variable. – Arnaud Denoyelle May 16 '16 at 12:05
  • @ArnaudDenoyelle, ah thanx, just always used the "base" word! Every day learn somthing new! – Evgeniy Mishustin May 16 '16 at 12:08
  • @ArnaudDenoyelle Ah ok, how? – swess May 16 '16 at 12:10
  • @ArnaudDenoyelle http://stackoverflow.com/q/37249947/5908129 – swess May 16 '16 at 12:11