Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
422 views
in Technique[技术] by (71.8m points)

java - Converting Hex answer to Dec?

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.

private class ConnectedThread extends Thread {
        private final InputStream mmInStream;
        private final OutputStream mmOutStream;
        private BluetoothSocket mmSocket;
        private ObdMultiCommand multiCommand;

        public ConnectedThread(BluetoothSocket socket) {
            connectionStatus = true;
            mmSocket = socket;
            InputStream tmpIn = null;
            OutputStream tmpOut = null;          

            try {
                tmpIn = socket.getInputStream();
                tmpOut = socket.getOutputStream();
            } catch (IOException e) {
                Log.v("e", "e");
            }

            mmInStream = tmpIn;
            mmOutStream = tmpOut;
        }

        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");
            }
        }

        private void OBDcmds() { // execute commands
            try {
                new TimeoutCommand(100).run(mmInStream, mmOutStream);
                new EchoOffCommand().run(mmInStream, mmOutStream);
                new LineFeedOffCommand().run(mmInStream, mmOutStream);
                new SelectProtocolCommand(ObdProtocols.ISO_15765_4_CAN).run(mmInStream, mmOutStream); //ISO_15765_4_CAN

                guiHandler(Constants.TOAST, Constants.SHORT, "Init Done");

            } catch (Exception e) {
                guiHandler(Constants.TOAST, Constants.SHORT, "Init Failed");
                // handle errors
            }
        }
    }

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 that look in my code?

Edit:

So because the value after the 6 numbers changes depending on the current of the battery of the car (that's what this command does), and because it's in a while-loop, i'm kind of lost on what the solution would be. Other commands only have 3 last numbers that needs to be counted for, so it's only the 6 first numbers that needs to be ignored when "calculating" the answer.

Edit 2:

I added a bit more code to hopefully show more context. All this is in a non-gui class. This class handles everything Bluetooth related and such, and in my other class "MainActivity" handles the gui and what not. Hopefully this will help a bit maybe. OdbRawCommand ANS= new OdbRawCommand("22 40 28"); This is from the pires obd-java API. And OdbRawCommand is being used to send custom commands like i'm doing right here. Here's what OdbRawCommand contains.

Edit 3:

Sorry about the many edits. but would it work if I modified the OdbRawCommand class that's in the API. Since getFormattedResult prints out the raw answer it gets from the car. Could I edit the API OdbRawCommand class (if possible) and do something like this? But by the looks of it the file is locked and can't by edited?

enter image description here

@Override
    public String getFormattedResult() {
        int val = Integer.parseInt(getFormattedResult().substring(6), 16); //added this line
        return getResult();
    }

I don't have my car today to test this, i'll get it tomorrow, but would this work?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use String#substring to extract the final four digits (call that s), followed by

Integer.parseInt(s, 16)

to convert that to an integer. The 16 tells the parser that the s is hexadecimal.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...