解码十六进制字符串

7
这是一个更大的问题的一部分,我们需要将一个旧应用程序移植到新设备上(当然也需要使用另一种编程语言/框架)。经过很多努力(嗅探通信线路,反向工程传输数据大约2-3周),我设法缩小了范围,找到了我认为包含2个数字的4个字节。其中一个数字是温度读数,如30.51、30.46等,如表01中第一列所示。另一个值是浮点数,可以在3.9到3.6(甚至更低,带有4位小数)的范围内。这4个十六进制字节(表01的第2列)应该包含这两个值。由于没有文档或源代码,我不得不进行一些反向工程。我设法缩小了范围,找到了我认为解码十六进制字符串为2个数字的Java代码部分。是否有人能够检查代码是否符合我的想法?我不是Java程序员,我主要处理其他编程语言。所以我需要几件事情:

  1. 附加的代码是否负责将十六进制转换为3个浮点数?这是最重要的事情
  2. 如果可能的话,重新构建一些代码,使其可以在(https://www.compilejava.net/)上运行。这样我就可以尝试使用不同的十六进制数集测试算法。
  3. “如果可能”的话,请添加一些注释

表01

30.51 => 01:53:4e:98
30.46 => 01:53:8e:94
30.43 => 01:53:8e:91
30.39 => 01:53:8e:8e
30.39 => 01:53:4e:8e

12.36 => 01:52:88:b1
16.01 => 01:52:c9:cf
18.65 => 01:52:ca:a5
21.14 => 01:52:8b:74

如果需要任何信息,请告诉我,因为我已经花了很多时间来解决这个问题。如果需要,我可以记录更多数字。
顺便说一下,左边的数字是温度读数(以摄氏度为单位)。所以最后可能涉及乘法才能得出数字?我对此不是很确定,但我想提一下我对此的了解。
我没有时间学习Java来修复这个问题(我们极少涉及Java),而且我已经花了近一个月的时间。真的很感激任何帮助来清除这个障碍。这是解码十六进制转换为两个数字的Java代码。我通过逆向工程遗留应用程序得到的。请注意,我之前发布的反编译代码已被告知受到保护协议的覆盖,因此已删除。
哎呀,我犯了一个错误,没有提到最终需要将其插入Python程序 - 因为我提到它是一个非常庞大的Python项目的一部分,其中几乎所有内容都已编码并且运行良好。非常抱歉没有提到这一点(忘记添加Python标签)。我必须在Python中重写这个。

你有Table 1的更多行吗?在最大和最小相关值附近的蛋糕会有所帮助。此外,对于前两个字节,类似的地图也会有所帮助。 - Bob Jacobsen
1
你在这里提供的代码并没有帮助。方法a()看起来像是返回一个温度,但它却操作了四个四字节数组。方法m()只操作了一个四字节数组,但给出的示例输入它返回的值约为4.16。考虑到该方法中巧妙的位操作,猜测这些值是如何计算的毫无意义。这不像是对原始输入进行处理,而更像是故意混淆。 - Wladimir Palant
2
反编译的代码对我来说有点可疑。特别是,我正在查看的部分看起来像这样:for (;;) { return d3 + d1; if ((d3 < 34.1D) && (d3 >= 34.0D)) {...。换句话说,那个 for 循环完全没有用,因为第一个语句是一个 return 语句,而在 return 语句下面的所有内容都永远不会被执行。实际上,这甚至不应该编译通过(参见示例)。无论如何,看起来反编译器不起作用,或者代码已经通过能够跳过编译检查的混淆器运行了。 - Radiodef
1
@Radiodef,毫无疑问,反编译的源代码看起来很奇怪,但我唯一的希望是它能提供一点线索。任何可利用的信息都可以建立在这个基础上——因为目前已经陷入了死胡同。不过Bob Jacobsen的答案还是让我兴奋起来...它非常接近真相。但是我需要它在整个范围内精度都达到+/- 0.05度C。 - R.W
1
你能在你的代码上运行这个javap程序并添加结果吗? "javap -c YourClassNameHere.class" https://docs.oracle.com/javase/7/docs/technotes/tools/windows/javap.html - ProgrammersBlock
显示剩余12条评论
5个回答

3

左起第四列中的C、8和4值不会影响其他数值。这让我想到每个数值只传输了12位信息。也许这是来自ADC的整数原始输出?如果是这样,你可以用以下方法解码:

double valueOne(long input) {
    long x = input & 0xFFF;

    double m = (30.51 - 12.36) / (0xE98 - 0x8B1);
    int c = 0xE91;
    double b = 30.43;

    return m*(x-c)+b;
}

m*(x-c+b) 线性逼近是为了使其在看起来是操作点附近尽可能准确; 您可以根据需要调整m、c和b的值。

要处理其他值,请创建一个类似的程序,掩盖和移位以获得正确的12位并提供所需的m、c和b校准值。

以下是表1的值计算出的结果:

3:4e:98 30.514
3:8e:94 30.466
3:8e:91 30.430
3:4e:8e 30.393
3:4e:8e 30.393

2:88:b1 12.364
2:c9:cf 15.799
2:ca:a5 18.370
2:8b:74 20.856

最低值并不完全准确;也许从12.36点以外的另一个值进行外推会更好(请参见m值的公式)。或者这是热电偶数据,因为它不太线性。需要了解有关实际电路的更多信息才能对此发表评论。

这些数据与温度传感器有关,不能排除它可能是原始输出。不幸的是,没有任何关于如何推导数字的文档,也没有关于传感器本身的文档。但是这4个字节都携带了2个值(电压和温度)。我们知道较大数字(温度)的精度为±0.05。 - R.W
我不熟悉Java,但如果您查看最终“if”条件的else部分,则可能会得到一些线索。 - R.W
发布的Java代码是我对二进制文件进行反汇编后得到的...因此,除非我是Java方面的专家,否则很难指出由于反编译本身导致的错误/漏洞。 - R.W
同样的4个字节还包含另一个值,即电压。电压应该是类似于3.xxxx(最大可能为3.985)。 - R.W
这是什么温度传感器?我怀疑,但有可能之前有人曾经使用过这个特定的物品。 - Joran Beasley
嗨@JoranBeasley,上面绝对没有任何标记...我只是解剖了外壳的一部分,并发现了这个(https://drive.google.com/file/d/13fp0jYoiJxgreKugUjFvhmnRdMpPJQ_V/view?usp=sharing)。我还四处看了看,发现旁边似乎有一个TI CC254X(很可能是CC2541)。 - R.W

3

反编译代码确实可以计算温度:

正如您所怀疑的那样,反编译的代码 class a 能够计算温度。方法 double a() 具有该算法。尽管反编译的代码无法编译,但经过一番努力,已经进行了修正(见下文),并根据您的输入和期望值准确地计算出温度。

结果:(使用 mapKey=77)

30.506 => 01:53:4e:98
30.460 => 01:53:8e:94
30.425 => 01:53:8e:91
30.391 => 01:53:8e:8e
30.391 => 01:53:4e:8e
12.338 => 01:52:88:b1
15.990 => 01:52:c9:cf
18.636 => 01:52:ca:a5
21.127 => 01:52:8b:74

校准:

方法a(),现在称为calculateTemperature(),如果您选择使用它,则似乎已经内置了校准。在4个参数中,第一个是十六进制字符串(算法仅使用其中两个八位字节),其他三个可以校准结果。很难知道如何使用这些值。但是,如果像我在main()中所示那样将它们保留为默认值,则计算出的温度是正确的。也许您知道为什么要校准结果。其中一个校准需要传递一个值介于65和85之间(我使用的是77)。还有另一个类似的参数,它会导致结果的乘法(零将忽略该参数)。我发现使用mapKey=77会产生结果稍微偏低(但78稍微偏高)。我发现使用100175的比例使结果更接近预期值。第三个校准是+/-类型的校准(零将忽略该参数)。

可能的电压计算

在计算出温度后,有一些代码将在温度在33.2到36.0之间时分配大约0.2-2.0的值。我称之为电压,但不知道它是什么。对于您的输入值,温度始终低于33.2,因此代码的这部分实际上是无用的。

方法c()的未知含义

有一个小方法c()。它为您提供的值返回大约4.16-4.18的值。这对您有任何意义吗?

工作代码:

main()方法演示了如何从十六进制字符串获取温度,并打印输入数据的结果。该代码在您提到的网站上运行。值得注意的是,3个校准参数不需要是byte[]数组,并且可以更简单地理解和使用为类型int。如果您对代码有任何疑问,请在评论区(或聊天中)与我联系,我会向您解释。

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Meter {

private List<Double> parameterList = new ArrayList<>();     // Used by the temperature algorithm
private Map<Integer, Double> scaleMap = new HashMap<>(21);  // Used to scale (not necessarily required)

public static void main(String[] args) {
    Meter meter = new Meter();
    meter.initData();

    //30.51 => 01:53:4e:98
    //30.46 => 01:53:8e:94
    //30.43 => 01:53:8e:91
    //30.39 => 01:53:8e:8e
    //30.39 => 01:53:4e:8e

    //12.36 => 01:52:88:b1
    //16.01 => 01:52:c9:cf
    //18.65 => 01:52:ca:a5
    //21.14 => 01:52:8b:74

    // Test each of the provided hex values

    int mapKey = 77;    // 77 seemed the best value; 78 was a bit too high

    String[] values = { "01:53:4e:98", "01:53:8e:94", "01:53:8e:91", "01:53:8e:8e",
            "01:53:4e:8e", "01:52:88:b1", "01:52:c9:cf", "01:52:ca:a5", "01:52:8b:74" };

    ByteBuffer key = ByteBuffer.allocate(4).order(ByteOrder.LITTLE_ENDIAN).putInt(mapKey);
    ByteBuffer scale = ByteBuffer.allocate(4).order(ByteOrder.LITTLE_ENDIAN).putInt(0);     // A number around 100175 perhaps provides better results
    ByteBuffer offset = ByteBuffer.allocate(4).order(ByteOrder.LITTLE_ENDIAN).putInt(0);    // No offset

    for (int i=0; i<values.length; i++) {
        double tempC = meter.calculateTemperature(hexStringToByteArray(values[i]), key.array(), scale.array(), offset.array());
        System.out.printf("%2.3f => %s\n", tempC, values[i]);
    }

}

/**
 * Convert a hex string (which may contain the `:` character) to a byte array
 * @param hexString 4 octets of the form xx:xx:xx:xx
 * @return The byte array
 */
static byte[] hexStringToByteArray(String hexString) {
    hexString = hexString.replaceAll(":", "");
    int len = hexString.length();
    byte[] data = new byte[len / 2];
    for (int i = 0; i < len; i += 2) {
        data[i / 2] = (byte) ((Character.digit(hexString.charAt(i), 16) << 4)
                             + Character.digit(hexString.charAt(i+1), 16));
    }
    return data;
}

/**
 * Populate the algorithm parameters (required), and the scaling factors (not necessarily required)
 */
private void initData() {

    scaleMap.put(65, 29.629);
    scaleMap.put(66, 29.660);
    scaleMap.put(67, 29.691);
    scaleMap.put(68, 29.722);
    scaleMap.put(69, 29.753);
    scaleMap.put(70, 29.784);
    scaleMap.put(71, 29.815);
    scaleMap.put(72, 29.846);
    scaleMap.put(73, 29.877);
    scaleMap.put(74, 29.908);
    scaleMap.put(75, 29.939);
    scaleMap.put(76, 29.970);
    scaleMap.put(77, 30.001);
    scaleMap.put(78, 30.032);
    scaleMap.put(79, 30.063);
    scaleMap.put(80, 30.094);
    scaleMap.put(81, 30.125);
    scaleMap.put(82, 30.156);
    scaleMap.put(83, 30.187);
    scaleMap.put(84, 30.218);
    scaleMap.put(85, 30.249);

    parameterList.add(52.94);
    parameterList.add(49.61);
    parameterList.add(46.51);
    parameterList.add(43.62);
    parameterList.add(40.94);
    parameterList.add(38.44);
    parameterList.add(36.12);
    parameterList.add(33.95);
    parameterList.add(31.93);
    parameterList.add(30.05);
    parameterList.add(28.29);
    parameterList.add(26.61);
    parameterList.add(25.05);
    parameterList.add(23.59);
    parameterList.add(22.23);
    parameterList.add(20.96);
    parameterList.add(19.76);
    parameterList.add(18.65);
    parameterList.add(17.60);
    parameterList.add(16.63);
    parameterList.add(15.71);
    parameterList.add(14.84);
    parameterList.add(14.02);
    parameterList.add(13.25);
    parameterList.add(12.53);
    parameterList.add(11.86);
    parameterList.add(11.22);
    parameterList.add(10.63);
    parameterList.add(10.07);
    parameterList.add(9.541);
    parameterList.add(9.046);
    parameterList.add(8.572);
    parameterList.add(8.126);
    parameterList.add(7.706);
    parameterList.add(7.311);
    parameterList.add(6.938);
    parameterList.add(6.588);
    parameterList.add(6.257);
    parameterList.add(5.946);
    parameterList.add(5.651);
    parameterList.add(5.374);
    parameterList.add(5.109);
    parameterList.add(4.859);
    parameterList.add(4.623);
    parameterList.add(4.400);
    parameterList.add(4.189);
    parameterList.add(3.990);
    parameterList.add(3.801);
    parameterList.add(3.623);
    parameterList.add(3.454);
    parameterList.add(3.294);
    parameterList.add(3.141);
    parameterList.add(2.996);
    parameterList.add(2.858);
    parameterList.add(2.728);
    parameterList.add(2.604);
    parameterList.add(2.487);
    parameterList.add(2.376);
    parameterList.add(2.270);
    parameterList.add(2.170);
    parameterList.add(2.075);
    parameterList.add(1.984);
    parameterList.add(1.897);
    parameterList.add(1.815);
    parameterList.add(1.737);
    parameterList.add(1.662);
    parameterList.add(1.591);
    parameterList.add(1.524);
    parameterList.add(1.459);
    parameterList.add(1.398);
    parameterList.add(1.340);
    parameterList.add(1.284);
    parameterList.add(1.231);
    parameterList.add(1.180);
    parameterList.add(1.132);
    parameterList.add(1.086);
    parameterList.add(1.042);
    parameterList.add(1.000);
    parameterList.add(0.9599);
    parameterList.add(0.9216);
    parameterList.add(0.8851);
    parameterList.add(0.8501);
    parameterList.add(0.8168);
    parameterList.add(0.7849);
    parameterList.add(0.7545);
    parameterList.add(0.7254);
    parameterList.add(0.6974);
    parameterList.add(0.6707);
    parameterList.add(0.6451);
    parameterList.add(0.6207);
    parameterList.add(0.5973);
    parameterList.add(0.5743);
    parameterList.add(0.5523);
    parameterList.add(0.5313);
    parameterList.add(0.5112);
    parameterList.add(0.4920);
    parameterList.add(0.4736);
    parameterList.add(0.4560);
    parameterList.add(0.4392);
    parameterList.add(0.4230);
    parameterList.add(0.4076);
    parameterList.add(0.3925);
    parameterList.add(0.3781);
    parameterList.add(0.3642);
    parameterList.add(0.3510);
    parameterList.add(0.3383);
    parameterList.add(0.3261);
    parameterList.add(0.3144);
    parameterList.add(0.3032);
    parameterList.add(0.2925);
    parameterList.add(0.2822);
    parameterList.add(0.2722);
    parameterList.add(0.2626);
    parameterList.add(0.2534);
    parameterList.add(0.2445);
    parameterList.add(0.2360);
    parameterList.add(0.2279);
    parameterList.add(0.2201);
    parameterList.add(0.2126);
    parameterList.add(0.2054);
    parameterList.add(0.1984);
    parameterList.add(0.1917);
    parameterList.add(0.1852);
    parameterList.add(0.1790);
    parameterList.add(0.1731);
    parameterList.add(0.1673);
    parameterList.add(0.1618);
    parameterList.add(0.1564);
    parameterList.add(0.1513);
    parameterList.add(0.1464);
    parameterList.add(0.1416);
    parameterList.add(0.1370);
    parameterList.add(0.1326);
    parameterList.add(0.1283);
    parameterList.add(0.1242);
    parameterList.add(0.1203);
    parameterList.add(0.1164);
    parameterList.add(0.1128);
    parameterList.add(0.1092);
    parameterList.add(0.1058);
    parameterList.add(0.1026);

}

/**
 * 
 * @param b1array The hex number b1:b2:b3:b4 (as a byte array)
 *            - The only bits used are b2 (6 low bits & x3f) and b3
 *            (all 8 bits & xff)
 * @param mapKey
 *            - Value from 65 to 85; if 77 then scale is 1.0; otherwise < 77
 *            or > 77 causes scaling
 * @param byte1Scale
 *            - Equal to zero (scale=1), or units in micro (10E6) where
 *            scale=value/10E6
 * @param byte1Offset
 *            - Measured in 10E6 (micro) - offset amount
 * @return The temperature in degrees Celsius
 */
public double calculateTemperature(byte[] b1array, byte[] mapKey, byte[] byte1Scale, byte[] byte1Offset) {

    double scale;
    int scaleMicroValue = ByteBuffer.wrap(byte1Scale).order(ByteOrder.LITTLE_ENDIAN).getInt();
    if (scaleMicroValue == 0) {
        scale = 1.0D;
    } else {
        scale = scaleMicroValue / 1000000.0D;
    }

    double offsetValue = ByteBuffer.wrap(byte1Offset).order(ByteOrder.LITTLE_ENDIAN).getInt() / 1000000.0D;

    /* 14 bits: b2_5 b2_4 ... b2_0 b3_7 .. b3_0 */
    byte byte2 = b1array[2];
    byte byte3 = b1array[3];
    int bitValue = (byte3 & 0xFF | (byte2 & 0x3F) << 8);
    double scaledBitValue = bitValue * scale - offsetValue;

    int key = (byte) (mapKey[0] & 0xFF);
    double mapValue = scaleMap.containsKey(key) ? scaleMap.get(key) : scaleMap.get(77);

    double param1 = 0.0;
    double param2 = parameterList.get(0);
    double result = 33.0D / scaledBitValue * (8191.0D - scaledBitValue) / mapValue;

    int i = 0;
    int j = parameterList.size();
    double minParameter = parameterList.get(j - 1);

    if (param2 < result || minParameter > result)
        return 0;

    int index = 0;
    boolean process = true;
    while (i < j && process) {
        if (result >= parameterList.get(i)) {
            if (i == 0) {
                param1 = parameterList.get(i);
                param2 = parameterList.get(i + 1);
                index = i;
                process = false;
            }
            if (process) {
                param1 = parameterList.get(i - 1);
                param2 = parameterList.get(i);
                index = i - 1;
            }
            process = false;
        }
        if (process)
            i++;
    }
    if (process) {
        index = 0;
        param2 = 0;
    }

    double voltage = 0.0;   // I don't even know if this is voltage (but it is only calculated if temp between 33.2 and 36
    double tempC = index + (result - param1) / (param2 - param1) - 40.0D;

    if ((tempC < 34.0D) && (tempC >= 33.2D)) {
        voltage = 1.95D;
    }

    while (true) {
        if ((tempC < 34.1D) && (tempC >= 34.0D)) {
            voltage = 1.881D;
        } else if ((tempC < 34.2D) && (tempC >= 34.1D)) {
            voltage = 1.805D;
        } else if ((tempC < 34.3D) && (tempC >= 34.2D)) {
            voltage = 1.71D;
        } else if ((tempC < 34.4D) && (tempC >= 34.3D)) {
            voltage = 1.615D;
        } else if ((tempC < 34.5D) && (tempC >= 34.4D)) {
            voltage = 1.52D;
        } else if ((tempC < 34.6D) && (tempC >= 34.5D)) {
            voltage = 1.4249999999999998D;
        } else if ((tempC < 34.7D) && (tempC >= 34.6D)) {
            voltage = 1.3299999999999998D;
        } else if ((tempC < 34.8D) && (tempC >= 34.7D)) {
            voltage = 1.2349999999999999D;
        } else if ((tempC < 34.9D) && (tempC >= 34.8D)) {
            voltage = 1.14D;
        } else if ((tempC < 35.0D) && (tempC >= 34.9D)) {
            voltage = 1.045D;
        } else if ((tempC < 35.1D) && (tempC >= 35.0D)) {
            voltage = 0.95D;
        } else if ((tempC < 35.2D) && (tempC >= 35.1D)) {
            voltage = 0.855D;
        } else if ((tempC < 35.3D) && (tempC >= 35.2D)) {
            voltage = 0.76D;
        } else if ((tempC < 35.4D) && (tempC >= 35.3D)) {
            voltage = 0.6649999999999999D;
        } else if ((tempC < 35.5D) && (tempC >= 35.4D)) {
            voltage = 0.57D;
        } else if ((tempC < 35.6D) && (tempC >= 35.5D)) {
            voltage = 0.475D;
        } else if ((tempC < 35.7D) && (tempC >= 35.6D)) {
            voltage = 0.38D;
        } else if ((tempC < 35.8D) && (tempC >= 35.7D)) {
            voltage = 0.285D;
        } else if ((tempC < 35.9D) && (tempC >= 35.8D)) {
            voltage = 0.19D;
        } else {
            if (tempC >= 36.0D) {
                break;
            }
            if (tempC < 35.9D) {
                break;
            }
            voltage = 0.095D;
        }
    }
    return tempC;
}

/**
 * I don't know what this function is:  It always calculates around 4.16 - 4.18
 * @param bArray The hex number b1:b2:b3:b4 (as a byte array)
 * Uses: 
 * byte0:  Low 2 bits
 * byte1:  all bits
 * byte2:  high 2 bits
 * @return I don't know the significance of this value
 */
public double m(byte[] bArray) {
    int byte2 = bArray[2];
    int byte1 = bArray[1];
    int byte0 = bArray[0];
    return 0.003077674645823156D * (((byte0 & 0x3) << 2 | (byte1 & 0xC0) >> 6) << 8
            | (byte2 & 0xC0) >> 6 | (byte1 & 0x3F) << 2);
}
}

编辑:接受十六进制字符串作为参数,并输出温度的方法

/**
 * Arg[0] is expected to be the input (a hex string)
 * The output is the temperature (printed to the console)
 * @param args One value; The hex string
 */
public static void main(String[] args) {

    Meter meter = new Meter();
    meter.initData();
    int mapKey = 77;    // Scaling factor
    ByteBuffer key = ByteBuffer.allocate(4).order(ByteOrder.LITTLE_ENDIAN).putInt(mapKey);
    ByteBuffer scale = ByteBuffer.allocate(4).order(ByteOrder.LITTLE_ENDIAN).putInt(1000180);       // No scaling
    ByteBuffer offset = ByteBuffer.allocate(4).order(ByteOrder.LITTLE_ENDIAN).putInt(0);    // No offset

    byte[] hexString = hexStringToByteArray(args[0]);

    double tempC = meter.calculateTemperature(hexString, key.array(), scale.array(), offset.array());
    System.out.printf("%.2f", tempC);

}

哇!这是我收到的最好的答案之一。在我消化整个内容之前,我想感谢您,并就您提到的问题进行澄清 - 1. 为什么需要校准 A)在“解码”未在整个范围内进行得很好时,有一个怀疑 - 这个怀疑是否是这些值原始adc / 来自热电偶等。2)至于方法c()和大约4.16-4.18左右的值,那些接近电压值。我没有得到电压的上限,但在“满”时它大约/高于3.985。 - R.W
我要休息了,明天很乐意回答你可能有的任何问题。 - Ian Mc
我稍微调整了校准值,发现可以让发布的结果更接近预期值。映射键77似乎是最好的选择(虽然78也很接近,但会导致结果略高),但结果略低,通过使用大约100175的比例,这会略微增加值。没有问题吗?你能将这段代码移植到Python吗? - Ian Mc
我得到的3.xxxx值应该是电压,因为它非常接近给定的范围。但不幸的是,上限从未被提及。但是,上限应该在>= 3.985左右。当我监视读数并连接适配器时,我可以看到电压增加,反之亦然,当我断开适配器时。所以那部分似乎工作正常。我已经在我的脚本中添加了如何计算电压(使用所有4个字节)。 - R.W
让我们在聊天中继续这个讨论 - R.W
显示剩余2条评论

2

我在Quora上看到了这篇文章。

你要求提供可以在Java中运行的反编译十六进制代码。我不清楚你从哪里得到这段代码。但是我看到上面的代码,发现最后一个函数将字节数组转换为双精度浮点数,并进行了一些尝试。你可以看到它不起作用,甚至总是以错误的方式得出温度。

附带的调试函数可用于比较位值。

也许其他位需要以某种方式与这个双精度浮点数交互。

此外,您确定您的示例全部正确吗?通常,这里的被除数是相似的,但有些差别很大。(运行程序!)如果这段代码是合法的,我希望至少在误差范围内得到相同的被除数。然后只需要调整用于乘以位的常数即可。

请注意,C++中的这些操作与Java相同。主要区别在于,在C++中,类型转换的语法更简单(即没有ByteBuffer语法或类库)。在Java中,一个字节只是一个8位有符号整数。由于它是一个整数,因此有几个隐式类型转换很方便。

此代码可在https://www.compilejava.net/上运行。

要进行其他测试,只需向main(...)方法添加代码即可。

抱歉,我不知道为什么这个复制的空格。

import java.nio.ByteBuffer;



public class CrazyConvertor {



//        int t = Integer.decode("0x01");

//        System.out.println("test is "+t+"\t01");

//        t = Integer.decode("0x24");

//        System.out.println("test is "+t+"\t36");



    //30.51 => 01:53:4e:98

    //30.46 => 01:53:8e:94

    //30.43 => 01:53:8e:91

    //30.39 => 01:53:8e:8e

    //30.39 => 01:53:4e:8e



    //12.36 => 01:52:88:b1

    //16.01 => 01:52:c9:cf

    //18.65 => 01:52:ca:a5

    //21.14 => 01:52:8b:74

    public static double[] temperatures;

    public static int[] encodedBytes;

    static {

        temperatures = new double[9];

        encodedBytes = new int[9];



        encodedBytes[0] = Integer.decode("0x01534e98");

        encodedBytes[1] = Integer.decode("0x01538e94");

        encodedBytes[2] = Integer.decode("0x01538e91");

        encodedBytes[3] = Integer.decode("0x01538e8e");

        encodedBytes[4] = Integer.decode("0x01534e8e");

        encodedBytes[5] = Integer.decode("0x015288b1");

        encodedBytes[6] = Integer.decode("0x0152c9cf");

        encodedBytes[7] = Integer.decode("0x0152caa5");

        encodedBytes[8] = Integer.decode("0x01528b74");



        temperatures[0] = 30.51;

        temperatures[1] = 30.46;

        temperatures[2] = 30.43;

        temperatures[3] = 30.39;

        temperatures[4] = 30.39;

        temperatures[5] = 12.36;

        temperatures[6] = 16.01;

        temperatures[7] = 18.65;

        temperatures[8] = 21.14;

    }



    static int MASK = 0x0003ffc0;



    public static void main(String[] args)

    {





        for (int k=0; k<9; k++){

            double f = fourBytesToDouble(encodedBytes[k]);

            int x = integerElegantConversion(encodedBytes[k]);

            System.out.println("Converted double            \t"+f);

            System.out.println("Converted double elegant    \t"+fourBytesElegantConversion(encodedBytes[k]));

            System.out.println("Target value\t"+temperatures[k]);

            System.out.println("Dividend\t"+temperatures[k]/f);

            System.out.println("Dividend temp by integer\t"+temperatures[k]/x);

            System.out.println();

        }



    }



    /**

     * takes four bytes

     * function identical to m below, but easier to read

     *

     * Key programmer's note: in java, a byte is a 8-bit integer

     * So this is basically dynamic type conversion from encoded bits to a double.

     *

     * @param z  and integer representing 4 bytes

     * @return

     */



    public static double fourBytesToDouble(int z){

        //read 12 bits from middle of 3 byte array

        byte[] bytes = ByteBuffer.allocate(4).putInt(z).array();



        //grab 4 bits from first byte from bits 0 thru 1 and bits 6 thru 7 of byte 2

        int x = ((bytes[1] & 0x3) << 2 | (bytes[2] & 0xC0) >> 6);

        x = x << 8;



        //grab second byte from bits  2 thru 7 of byte 1 and bits 0 thru 1 of byte 2

        int y = (bytes[2] & 0x3F) << 2 | (bytes[3] & 0xC0) >> 6;

        int r = x | y;   //or the bytes, so you obtain 12 bits; four from byte 1 and eight from byte 2

        System.out.println("  fourBytesToDouble (int)         \t"+r);

        return 0.003077674645823156D *  new Double(r);

    }



    /**

     * original reference function

     * @param paramArrayOfByte

     * @return

     */

    public static double m(byte[] paramArrayOfByte)

    {

        int i = paramArrayOfByte[2];

        int j = paramArrayOfByte[1];

        return 0.003077674645823156D * (((paramArrayOfByte[0] & 0x3) << 2 | (paramArrayOfByte[1] & 0xC0) >> 6) << 8 | (i & 0xC0) >> 6 | (j & 0x3F) << 2);

    }



    /**

     * Same task using integer masking.

     * Probably the original code that got obfuscated by compiler

     * @param x

     * @return

     */

    public static double fourBytesElegantConversion(int x){

////        ByteBuffer bb = ByteBuffer.allocate(4).put(b);

////        bb.position(0);

////        int x = (bb.getInt() << 10) >> 10;

////        System.out.println("   x\t"+fourBytesBinaryString(x));

////        System.out.println("mask\t"+fourBytesBinaryString(MASK));

//        x = x & MASK;

////        System.out.println("  x1\t"+fourBytesBinaryString(x));

//        x = x >> 6;



        x = (x & MASK) >> 6;

        System.out.println("  fourBytesElegantConversion (int)\t"+x);

        return 0.003077674645823156D *  new Double(x);

    }



    public static int integerElegantConversion(int x) {

        return (x & MASK) >> 6;

    }

    /**

     * helper function to compare bits

     * @param x

     * @return

     */

    public static String fourBytesBinaryString(int x){

        String r = Integer.toBinaryString(x);

        int extra = 32 - r.length();



        for (int i=0; i < extra; i++){

            r = "0"+r;

        }

        return r;

    }



    /**

     * helper function to compare bits

     * @param b

     * @return

     */

    public static String eightBytesBinaryString(byte[] b){

        String r = oneByteBinaryString(b[0]);

        for (int k=1; k < 8; k++)  r += oneByteBinaryString(b[k]);

        return r;

    }



    /**

     * helper function to compare bits

     * @param b

     * @return

     */

    public static String fourBytesBinaryString(byte[] b){

        String r = oneByteBinaryString(b[0]);

        for (int k=1; k < 4; k++) r += oneByteBinaryString(b[k]);

        return r;

    }



    /**

     * helper function to compare bits; helper function to above

     * @param b

     * @return

     */

    private static String oneByteBinaryString(byte b) {

        String r = Integer.toBinaryString(b);



        if (b < 0){

            r = r.substring(24);

        } else {

            int extra = 8 - r.length();



            for (int i = 0; i < extra; i++) {

                r = "0" + r;

            }

        }

        return r;

    }

}

编辑:由于复制/粘贴造成的拼写错误


我有一个想法。你提到了第二个电压,大小约为4。那么,我需要将这个数字乘以1到3范围内的数字才能得到正确的答案。也许我们需要那个数字?显然它不在这里,但我敢打赌它在某个4字节块中。 - BAMF4bacon
我不知道传感器背后的原理。有人能否评论一下,根据设计,将其乘以电压是否有意义? - BAMF4bacon
嗨@BAMF4bacon,太棒了!顺便说一下,你看到的代码是我通过反编译得到的,因为没有源代码可用。我正在使用Python进行这些操作,但感谢您提供的Java代码,非常感谢。我会逐步查看您提供的所有内容。在您几小时前发布的帖子中,该数字也应该在这4个字节中,因为在屏幕更新时,实际上没有传输任何其他数据(甚至没有一个位!)-所以它就隐藏在那里 :) - R.W

2
如果你想反编译字节码,只需使用反编译器。你可以使用这个页面或在IDE中使用JD插件。IntelliJ IDEA或Eclipse有内置的反编译器,只需在其中打开.class文件并阅读反编译代码即可。
如果你只想解码一些十六进制字符串,而不是字节码,请使用这个网站
顺便说一句,如果你真的想构建自己的十六进制字符串解码器,我认为使用一堆if else语句不是一个好主意。十六进制编码/解码只是将一些位移动到周围的过程。使用<<>>运算符会使你的代码更短,更易于阅读。也许这篇文章能帮助你。

嗨,很酷的链接。非常感谢。我之前尝试了一堆反编译器,但没有一个做得好。据我所知,代码并没有混淆,但在使用反编译器运行时,它们在不同的位置断开。可能与框架有关而不是其他任何东西。我会阅读这些链接,它们可能会给我提供所需的线索。 - R.W

2

因为没有文档和源代码,我不得不进行一些逆向工程。

Java 的好处是它可以“轻松地”被反编译。

既然你正在将某个东西移植到一个新设备上,我假设你有权利去反编译并查看源代码。

有一些网站像这个可以帮助你很多。

这可能不能直接解决问题,但希望你能够找出他们如何编码 :)


嗨张,是的 - 只要在公司内部完成,我们就可以操纵编译后的代码。我试用了包括你提到的那个在内的所有流行的反编译器。谢谢你的提示 :) 这变得越来越有趣,我感觉自己要不遗余力地解决这个问题,但可恶的时间不够用...哈哈。由于这主要涉及到解码一堆十六进制数,所以我甚至在 Quora 上发布了这个问题 - 猜猜怎么着?几天过去了,我还没有收到任何像样的回复! :) - R.W
Jacobsen 很快就接近了,但它必须在整个范围内工作,因为准确度(+/-0.05)非常关键。而且还需要从这 4 个字节中提取较小的数字(3.xxxx)。我不是一个严肃的数学家,并且这只是我正在处理的几个项目之一 - 所以我不能全天候工作,更不能每周七天...唉 - R.W

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接