如何找到消息中的大端密钥?

发布于 2025-01-15 16:36:38 字数 994 浏览 1 评论 0原文

我正在尝试使用代理从 ESP32 读取二进制消息;我写了一个 phyton 脚本,在其中订阅了该主题。我实际收到的消息是:

b'\x00\x00\x00?'

这是一个浮点二进制小端消息,但我不知道解码它的密钥。有没有办法根据这些数据找到解码密钥? 这是我的 python 代码:

    import paho.mqtt.client as mqtt


def on_connect1(client1,  userdata1, flags1, rc1):
    
    client1.subscribe("ESP32DevKit123/mytopic")

def on_message1(client1, userdata1, msg1):
    print(msg1.topic+" "+ "TESTENZA: "+str(msg1.payload))

client1 = mqtt.Client()

client1.username_pw_set(username="myuser",password="mypassword")

client1.on_connect = on_connect1

client1.on_message = on_message1

client1.connect("linkclient", portnumber, 60)


def twosComplement_hex(hexval):
    bits = 16 # Number of bits in a hexadecimal number format
    on_message1 = int(hexval, bits)
    if on_message1 & (1 << (bits-1)):
    on_message1 -= 1 << bits
    return on_message1


client1.loop_forever()

它还在 on_message1 -= 1 << 行中给了我一个错误位;错误显示:预期的预期块 pylance。有什么解决办法吗?

I am trying to read a binary message from an ESP32 using a broker; i wrote a phyton script where I subscribe the topic. the message that i actually receive is:

b'\x00\x00\x00?'

this is a float binary little endian message but I don't the key to decode it. Is there a way to find the decode key based on this data?
This is my python code:

    import paho.mqtt.client as mqtt


def on_connect1(client1,  userdata1, flags1, rc1):
    
    client1.subscribe("ESP32DevKit123/mytopic")

def on_message1(client1, userdata1, msg1):
    print(msg1.topic+" "+ "TESTENZA: "+str(msg1.payload))

client1 = mqtt.Client()

client1.username_pw_set(username="myuser",password="mypassword")

client1.on_connect = on_connect1

client1.on_message = on_message1

client1.connect("linkclient", portnumber, 60)


def twosComplement_hex(hexval):
    bits = 16 # Number of bits in a hexadecimal number format
    on_message1 = int(hexval, bits)
    if on_message1 & (1 << (bits-1)):
    on_message1 -= 1 << bits
    return on_message1


client1.loop_forever()

It also gives me an error in the line on_message1 -= 1 << bits; the error says: Expected intended block pylance. Any solutions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

酒与心事 2025-01-22 16:36:38

您提供的数据是 b'\x00\x00\x00?' - 我假设这是 0000003f (请使用 msg1.txt 输出十六进制) Payload.hex())。

我还假设“浮动二进制小端”指的是大端浮点(IEE754) - 请注意,这与您正在使用的算法不匹配(两人恭维)。将此输入插入在线工具表明预期结果(“Float - Big Endian (ABCD)”)是8.82818e-44(值得用这个工具检查;有时编码可能不是你想象的那样 是!)。

让我们使用 python 解压它(有关更多信息,请参阅 struct 文档)

>>> from struct import unpack
>>> unpack('>f', b'\x00\x00\x00\x3f')[0]
8.828180325246348e-44

: :

  • [0] 之所以存在,是因为 unpack 返回一个数组(您可以从输入中解压多个项目)
  • >f - > 表示big-endian 和 f float(标准大小 = 4 字节)

您的原始代码给出错误“预期预期块”的原因是由于 on_message1 -= 行中缺少缩进1<<;位(因为它位于 if 后面,因此需要缩进)。该算法似乎与您的任务无关(但我可能遗漏了一些细节)。

The data you provided is b'\x00\x00\x00?' - I'm going to assume that this is 0000003f (please output hex with msg1.payload.hex()).

I'll also assume that by "float binary little endian" you mean a big endian floating point (IEE754) - note that this does not match up with the algorithm you are using (twos compliment). Plugging this input into an online tool indicates that the expected result ("Float - Big Endian (ABCD)") is 8.82818e-44 (it's worth checking with this tool; sometimes the encoding may not be what you think it is!).

Lets unpack this using python (see the struct docs for more information):

>>> from struct import unpack
>>> unpack('>f', b'\x00\x00\x00\x3f')[0]
8.828180325246348e-44

Notes:

  • The [0] is there because unpack returns an array (you can unpack more than one item from the input)
  • >f - the > means big-endian and the f float (standard size = 4 bytes)

The reason your original code gives the error "Expected intended block" is due to the lack of indentation in the line on_message1 -= 1 << bits (as it follows an if it needs to be indented). The algorithm does not appear relevant to your task (but there may be details I'm missing).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文