Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.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_Zigbee_Hexdump - Fatal编程技术网

python:打印十六进制数据而不是字典

python:打印十六进制数据而不是字典,python,zigbee,hexdump,Python,Zigbee,Hexdump,试图从Lowe的Iris智能开关解码Zigbee协议。我正在使用的API回调接收已经解析的帧,并作为(我相信)字典对象(?)发送到回调。我的代码: def callback(data): print data 打印以下内容: {'profile': '\xc2\x16', 'source_addr': '\x93\x0c', 'dest_endpoint': '\x02', 'rf_data': '\t\x00\x81\x00\x00', 'source_endpoint': '\x

试图从Lowe的Iris智能开关解码Zigbee协议。我正在使用的API回调接收已经解析的帧,并作为(我相信)字典对象(?)发送到回调。我的代码:

def callback(data):
    print data
打印以下内容:

{'profile': '\xc2\x16', 'source_addr': '\x93\x0c', 'dest_endpoint':
 '\x02', 'rf_data': '\t\x00\x81\x00\x00', 'source_endpoint': '\x02',
 'options': '\x01', 'source_addr_long': '\x00\ro\x00\x03\xbc\xdf\xab',
'cluster': '\x00\xef', 'id': 'rx_explicit'}
我认为这是按字节顺序的,我更喜欢这样的输出:

C2 16 93 0C 02 09 00 81 00 00 02 01 00 ...
使用Python内置函数,使用给定的“data”参数,有没有办法做到这一点?此外,我不知道如何将“\ro”解释为8位十六进制。我想“\t”是0x09

我真正想要的是数据帧的原始转储,但我不知道是否有API调用

使用Python内置函数,使用给定的“data”参数,有没有办法做到这一点

不。字典是任意排序的,因此无法知道值的顺序

此外,我不知道如何将“\ro”解释为8位十六进制

它是“\r”和“o”,即0x0d 0x6f

我想“\t”是0x09

我真正想要的是数据帧的原始转储,但我不知道是否有API调用


我们不知道,因为您还没有共享您首先使用的API。

如果没有空格,请在字符串上尝试内置的
encode
方法

所以
'\x0a\x0b\x0c'.encode('hex')
将产生
'0a0b0c'
。有许多was可以迭代字典值,但请注意,它本质上是一个无序的数据结构

也许像这样的事情就可以了

data = {'a':'\x0a\x0a\x0a', 'b':'\x0b\x0b\x0b'}
print '{' + ''.join( [ ('\'%s\'' %k) + ': \'%s\',' %data[k].encode('hex') for k in data ] ) + '}' 
这项工作:

def printData(data)
    str = "> "
    for d in data:
        for e in data[d]:
            str = str + "{0:02X}".format(ord(e)) + " "
    print (str)

我使用这个字符串生成器,所以我可以使用logging.info(str)而不是print…

+1来获得一个非常完整的答案。您将无法使用该字典获得数据帧的原始转储。ZigBee协议将集群ID以及源和目标端点分组在一起。检查您的API,看看是否有另一个对象可以作为字节数组引用。
def printData(data)
    str = "> "
    for d in data:
        for e in data[d]:
            str = str + "{0:02X}".format(ord(e)) + " "
    print (str)