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

5.9k Views Asked by At

How to decode:

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

to HEX or String in Python code.

2

There are 2 best solutions below

0
On BEST ANSWER

Maybe what you want to do is to convert it to bytes; below you can see how I used to write it to a binary file.

    localFile = open ('/home/youruser/data.bin', 'wb')
    try:
        for byteValue in arrayValue:
            localFile.write(chr(byteValue))
    finally:
        localFile.close()
1
On

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]'