Converting Hex answer to Dec?

删除回忆录丶 提交于 2019-11-30 09:31:31

问题


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?

@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?


回答1:


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.



来源:https://stackoverflow.com/questions/37249947/converting-hex-answer-to-dec

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!