[Tutor] dbus.Array to string

eryksun eryksun at gmail.com
Mon Aug 12 03:40:02 CEST 2013


On Sun, Aug 11, 2013 at 9:14 PM, Amit Saha <amitsaha.in at gmail.com> wrote:
> >>> import dbus
> >>> ssid=dbus.Array([dbus.Byte(66), dbus.Byte(105), dbus.Byte(103),
> dbus.Byte(80), dbus.Byte(111), dbus.Byte(110), dbus.Byte(100),
> dbus.Byte(54), dbus.Byte(55), dbus.Byte(57), dbus.Byte(68),
> dbus.Byte(56), dbus.Byte(53)], signature=dbus.Signature('y'),
> variant_level=1)
>
> >>> temp_ssid = ''.join([chr(byte) for byte in ssid])
> >>> temp_ssid
> 'BigPond679D85'

Typically strings should be unicode. If the byte sequence is Latin-1
(including ASCII), you can map unichr() and join the characters with
u''.join(); that's equivalent to chr() and ''.join() in 3.x. More
generally, decode() the byte string. A simply way that works in 2.6+
is to create a bytearray:

    >>> bytearray(ssid).decode('latin-1')
    u'BigPond679D85'


More information about the Tutor mailing list