将十六进制答案转换为十二月? [英] Converting Hex answer to Dec?

查看:140
本文介绍了将十六进制答案转换为十二月?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些帮助才能将我从汽车中的OBD适配器获得的答案转换为十进制,然后将转换后的任何值添加到公式并打印出来。

 私有类ConnectedThread扩展Thread {
private final InputStream mmInStream;
private final OutputStream mmOutStream;
私有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());

试试{
ANS.run(mmInStream,mmOutStream);
} catch(InterruptedException e){
e.printStackTrace();
}
}
} catch(例外e){
e.printStackTrace();
System.out.println(之前的内部捕获);
}
}

private void OBDcmds(){//执行命令
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(例外e){
guiHandler(Constants.TOAST,Constants.SHORT,Init Failed);
//处理错误
}
}
}

当被问到ANS会返回类似6240901F30的内容,其中第一个 6个数字与答案无关( 624090 1F30)。所以我们剩下的是最后一个(在这种情况下)4个数字(1F30),它们是十六进制的,需要转换为十进制。然后将该值添加到公式 x * 0,0625 - 512 中。它需要超过这六个第一个数字,否则答案是错误的。



我的代码会怎么样?



编辑:



因为6个数字后的值会根据汽车电池的电流而变化(这就是这个命令的作用),并且因为它是在一个循环中,我有点迷失在解决方案的基础上。其他命令只有3个需要计算的最后一个数字,所以在计算答案时,只需要忽略6个第一个数字。



编辑2 :



我添加了更多代码以希望显示更多上下文。所有这些都是非gui类。这个类处理蓝牙相关的所有东西,在我的其他类MainActivity处理gui和什么不是。希望这可能有点帮助。 OdbRawCommand ANS = new OdbRawCommand(22 40 28); 这是来自

  @Override 
public String getFormattedResult(){
int val = Integer.parseInt(getFormattedResult()。substring(6), 16); //添加了这一行
return getResult();
}

我今天没有车来测试这个,我会得到的明天会这样做吗?

解决方案

使用字符串#substring 提取最后四位数字(称为 s ),然后是



整数。 parseInt(s,16)



将其转换为整数。 16告诉解析器 s 是十六进制的。


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?

解决方案

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.

这篇关于将十六进制答案转换为十二月?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆