[Tutor] dbus.Array to string

Steven D'Aprano steve at pearwood.info
Mon Aug 12 04:34:37 CEST 2013


On 12/08/13 11:40, eryksun wrote:
> 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'


Nice!

But, what makes you think that the encoding will be latin-1? If I had to guess an encoding, I'd guess UTF-8.



-- 
Steven


More information about the Tutor mailing list