【问题标题】:Variable inside a byte string to change dynamically字节字符串内的变量动态更改
【发布时间】:2022-01-14 14:13:57
【问题描述】:

我在 python 中有这个字节字符串"\x02\x09\x88"

如何动态更改 88?

我试过了:

sts = bytes('\x02\x09\x{}'.format("88"), 'ascii')

我得到错误 unicodeescape codec cant decode bytes in position 8-9: truncated \nXX escape

但没有运气,我无法弄清楚。

请帮忙

【问题讨论】:

    标签: python byte


    【解决方案1】:

    尝试将字节串分为两部分。如果您的字符串采用 '88' 形式,请使用 int(s, 16) 进行转换:

    dynamic_part = '88'
    byte_element = int(dynamic_part, 16)
    sts = b'\x02\x09' + bytes([byte_element])
    print(sts)
    

    哪个打印:

    b'\x02\t\x88'
    

    【讨论】:

    • IT 并不总是 INT,有时它的 EA
    • 什么是 EA?那是一种数据类型吗?
    • 都是十六进制值
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-24
    • 1970-01-01
    • 2018-09-20
    • 1970-01-01
    相关资源
    最近更新 更多