Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python结构到原始字符串表示_Python_Hex - Fatal编程技术网

python结构到原始字符串表示

python结构到原始字符串表示,python,hex,Python,Hex,我想知道一些事情,现在我使用python ctypes制作一些wifi帧结构,使用lorcon2我可以通过Lan发送它们。我想把这个结构转换成一个字节字符串,得到这个结构的无符号十六进制表示。为此,我看到了两个功能可以实现这一点。ctypes.string_at和ctypes.wstring_at函数。我知道ctypes.wstring_at用于生成unicode字符串,但ctypes.string_at用于???我们用它能得到什么样的线??ascII字符串??或十六进制字符串?? 假设F()可

我想知道一些事情,现在我使用python ctypes制作一些wifi帧结构,使用lorcon2我可以通过Lan发送它们。我想把这个结构转换成一个字节字符串,得到这个结构的无符号十六进制表示。为此,我看到了两个功能可以实现这一点。ctypes.string_at和ctypes.wstring_at函数。我知道ctypes.wstring_at用于生成unicode字符串,但ctypes.string_at用于???我们用它能得到什么样的线??ascII字符串??或十六进制字符串??
假设F()可以将结构转换为无符号十六进制字节字符串:

d类(结构):
_字段(num,c_uint8),(char,c_char)]
s=d(num=129,char=c)
q=F(s)

如果我打印“q”,我想要这样的东西:
“\xe1\x63”
0xe1是十六进制的129
0x63是99的十六进制形式
其中“c”以ascII编码为99
同样地,我搜索另一个函数来获得“q”中每个元素的确切字节值,假设这个函数是wx(),那么它可以返回这个:
129如果写入:wx(q[0])
99如果我写:wx(q[1])

谢谢

不需要
F()
也不需要
wx()

class d(Structure):
  _fields_ = [("num",c_uint8),("char",c_char)]

  def __str__(self):
    return struct.pack('Bc', self.num, self.char)

  def __getitem__(self, ix):
    if ix == 0:
      return self.num
    if ix == 1:
      return ord(self.char)
    raise IndexError('structure index out of range')

这是我第一次看到str和getitem,在哪里可以了解它们?它们的用途是什么
class d(Structure):
  _fields_ = [("num",c_uint8),("char",c_char)]

  def __str__(self):
    return struct.pack('Bc', self.num, self.char)

  def __getitem__(self, ix):
    if ix == 0:
      return self.num
    if ix == 1:
      return ord(self.char)
    raise IndexError('structure index out of range')