• <legend id='yu2qH'><style id='yu2qH'><dir id='yu2qH'><q id='yu2qH'></q></dir></style></legend>
      <bdo id='yu2qH'></bdo><ul id='yu2qH'></ul>

  • <i id='yu2qH'><tr id='yu2qH'><dt id='yu2qH'><q id='yu2qH'><span id='yu2qH'><b id='yu2qH'><form id='yu2qH'><ins id='yu2qH'></ins><ul id='yu2qH'></ul><sub id='yu2qH'></sub></form><legend id='yu2qH'></legend><bdo id='yu2qH'><pre id='yu2qH'><center id='yu2qH'></center></pre></bdo></b><th id='yu2qH'></th></span></q></dt></tr></i><div id='yu2qH'><tfoot id='yu2qH'></tfoot><dl id='yu2qH'><fieldset id='yu2qH'></fieldset></dl></div>

      <small id='yu2qH'></small><noframes id='yu2qH'>

        <tfoot id='yu2qH'></tfoot>

      1. Python:将 HEX 字符串转换为字节

        时间:2023-09-28
      2. <legend id='fQS2A'><style id='fQS2A'><dir id='fQS2A'><q id='fQS2A'></q></dir></style></legend>

        <small id='fQS2A'></small><noframes id='fQS2A'>

          <bdo id='fQS2A'></bdo><ul id='fQS2A'></ul>

          • <tfoot id='fQS2A'></tfoot>
                    <tbody id='fQS2A'></tbody>
                  <i id='fQS2A'><tr id='fQS2A'><dt id='fQS2A'><q id='fQS2A'><span id='fQS2A'><b id='fQS2A'><form id='fQS2A'><ins id='fQS2A'></ins><ul id='fQS2A'></ul><sub id='fQS2A'></sub></form><legend id='fQS2A'></legend><bdo id='fQS2A'><pre id='fQS2A'><center id='fQS2A'></center></pre></bdo></b><th id='fQS2A'></th></span></q></dt></tr></i><div id='fQS2A'><tfoot id='fQS2A'></tfoot><dl id='fQS2A'><fieldset id='fQS2A'></fieldset></dl></div>
                  本文介绍了Python:将 HEX 字符串转换为字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试制作将通过 UDP 发送的字节帧.我有 Frame 类,它具有 syncframeSizedatachecksum 等属性. 我使用十六进制字符串来表示值.像这样:

                  I'm trying to make byte frame which I will send via UDP. I have class Frame which has attributes sync, frameSize, data, checksum etc. I'm using hex strings for value representation. Like this:

                  testFrame = Frame("AA01","0034","44853600","D43F")
                  

                  现在,我需要将这些十六进制值连接在一起并将它们转换为像这样的字节数组?!

                  Now, I need to concatenate this hex values together and convert them to byte array like this?!

                  def convertToBits(self):
                      stringMessage = self.sync + self.frameSize + self.data + self.chk
                      return b16decode(self.stringMessage)
                  

                  但是当我打印转换后的值时,我没有得到相同的值,或者我不知道正确读取 python 表示法:

                  But when I print converted value I don't get the same values or I don't know to read python notation correctly:

                  This is sync: AA01
                  This is frame size: 0034
                  This is data:44853600
                  This is checksum: D43F
                  
                  b'xaax01x004Dx856x00xd4?'
                  

                  所以,第一个单词转换正常 (AA01 -> xaax01) 但 (0034 -> x004D) 它不一样.我尝试使用 bytearray.fromhex 因为我可以在字节之间使用空格,但我得到了相同的结果.你能帮我通过 UDP 发送相同的十六进制字吗?

                  So, first word is converted ok (AA01 -> xaax01) but (0034 -> x004D) it's not the same. I tried to use bytearray.fromhex because I can use spaces between bytes but I got same result. Can you help me to send same hex words via UDP?

                  推荐答案

                  Python 将任何可以表示可打印 ASCII 字符的字节显示为该字符.4x34 相同,但它选择在表示中打印 ASCII 字符.

                  Python displays any byte that can represent a printable ASCII character as that character. 4 is the same as x34, but as it opted to print the ASCII character in the representation.

                  所以 x004 真的和 x00x34 一样,Dx856x00x44x85x36x00,而xd4?xd4x3f相同,因为:

                  So x004 is really the same as x00x34, Dx856x00 is the same as x44x85x36x00, and xd4? is the same as xd4x3f, because:

                  >>> b'x34'
                  '4'
                  >>> b'x44'
                  'D'
                  >>> b'x36'
                  '6'
                  >>> b'x3f'
                  '?'
                  

                  这只是字节值的表示;该值完全正确,您无需执行任何其他操作.

                  This is just the representation of the bytes value; the value is entirely correct and you don't need to do anything else.

                  如果有帮助,您可以使用 bytes 值可视化为十六进制" rel="nofollow">binascii.hexlify():

                  If it helps, you can visualise the bytes values as hex again using binascii.hexlify():

                  >>> import binascii
                  >>> binascii.hexlify(b'xaax01x004Dx856x00xd4?')
                  b'aa01003444853600d43f'
                  

                  您会看到 4D6? 再次由正确的十六进制字符.

                  and you'll see that 4, D, 6 and ? are once again represented by the correct hexadecimal characters.

                  这篇关于Python:将 HEX 字符串转换为字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:UDP 数据接收上的 Python 套接字错误.(10054) 下一篇:侦听 UDP 数据包的 Python 原始套接字;只收到一半的数据包

                  相关文章

                1. <small id='qHddA'></small><noframes id='qHddA'>

                  <tfoot id='qHddA'></tfoot>
                  <i id='qHddA'><tr id='qHddA'><dt id='qHddA'><q id='qHddA'><span id='qHddA'><b id='qHddA'><form id='qHddA'><ins id='qHddA'></ins><ul id='qHddA'></ul><sub id='qHddA'></sub></form><legend id='qHddA'></legend><bdo id='qHddA'><pre id='qHddA'><center id='qHddA'></center></pre></bdo></b><th id='qHddA'></th></span></q></dt></tr></i><div id='qHddA'><tfoot id='qHddA'></tfoot><dl id='qHddA'><fieldset id='qHddA'></fieldset></dl></div>

                    <legend id='qHddA'><style id='qHddA'><dir id='qHddA'><q id='qHddA'></q></dir></style></legend>
                      • <bdo id='qHddA'></bdo><ul id='qHddA'></ul>