Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to decode dbus.Array fromat to hex or string in python

How to decode:

dbus.Array([dbus.Byte(1), dbus.Byte(35)], signature=dbus.Signature('y'))

to HEX or String in Python code.

like image 666
lucifer Avatar asked Mar 11 '23 08:03

lucifer


1 Answers

As the DBus specification says, y means byte. So dbus.Array([...], signature=dbus.Signature('y')) is an array of bytes.

Let's consider this value:

value = dbus.Array([dbus.Byte(76), dbus.Byte(97), dbus.Byte(98), dbus.Byte(65), dbus.Byte(80), dbus.Byte(97), dbus.Byte(114), dbus.Byte(116)], signature=dbus.Signature('y'))
  • If you know your value contains a string:

    print("value:%s" % ''.join([str(v) for v in value]))
    # Will print 'value:LabAPart'
    
  • For an array of byte:

    print("value:%s" % [bytes([v]) for v in value])
    # Will print 'value:[b'L', b'a', b'b', b'A', b'P', b'a', b'r', b't']'
    
  • For an array of integer:

    print("value:%s" % [int(v) for v in value])
    # Will print 'value:[76, 97, 98, 65, 80, 97, 114, 116]'
    
like image 167
OlivierM Avatar answered May 11 '23 14:05

OlivierM