Byte-Array to String

Robert Rawlins - Think Blue robert.rawlins at
Fri Apr 20 10:51:42 CEST 2007

Morning Steve,


That stuff looks mighty promising, I did play around with the toString()
function yesterday but couldn't get the damned thing working. The syntax has
me a little muddled, perhaps you can help out on this by taking a look at my



import dbus


bus = dbus.SystemBus()

obj = bus.get_object('org.bluez', '/org/bluez')

obj = bus.get_object('org.bluez', '/org/bluez/hci0')

adapter = dbus.Interface(obj, 'org.bluez.Adapter')


# Search For Obex Push Protocol

result = adapter.GetRemoteServiceHandles('00:17:B0:A0:E7:09', 'opp')


result2 = adapter.GetRemoteServiceRecord('00:17:B0:A0:E7:09', result[0])


Now 'result2' is basically that byte array, Unfortunately the API doesn't
give any more information other than that the function returns a array{byte}
so I can't really shed any more light on that however if I can just get it
to output all the elements from that array then I will know which one it is
I'm looking for.


I tried to implement your suggestions below as they look pretty safe, but I
couldn't get it to work, my syntax is pretty confused as I'm so new to the
language. Like I say, there is only one element of the array I'm interested
in, I just need to see them all before I know which it is.


Thanks again Steve,




From: Steven Howe [mailto:howe.steven at] 
Sent: 19 April 2007 16:51
To: Robert Rawlins - Think Blue
Cc: python-list at
Subject: Re: Byte-Array to String


Robert Rawlins - Think Blue wrote: 

Hello Guys,
I have a byte array passed to me by dbus and I'm looking to convert it into
a string? Is that possible? Sorry for seeming like a putts with these
questions, I'm not used to all these complex data types :-D
The byte array looks something like this when printed to screen.
dbus.Array([dbus.Byte(54), dbus.Byte(0), dbus.Byte(24), dbus.Byte(9),
dbus.Byte(0), dbus.Byte(0), dbus.Byte(10), dbus.Byte(0), dbu
s.Byte(0), dbus.Byte(0), dbus.Byte(0), dbus.Byte(9), dbus.Byte(0),
dbus.Byte(1), dbus.Byte(53), dbus.Byte(3), dbus.Byte(25), dbus.
Byte(16), dbus.Byte(0), dbus.Byte(9), dbus.Byte(2), dbus.Byte(0),
dbus.Byte(53), dbus.Byte(3), dbus.Byte(9), dbus.Byte(1), dbus.By
te(1)], signature=dbus.Signature('y'))
Thanks again,

When reading about array, I wondered what the hell it was good for. Now I
see. It's a tool to build objects 
to pass to the Operating System or other applications. Something like
ctypes. The OS might store data from left to right, or right to left, or not
use IEEE standards (which VMS certainly doesn't). So the data you give/get
from the system call must be massaged by the application before it's usable.

python/lib/module-array.html (5.14 array -- Efficient arrays of numeric
array.tostring( )
Convert the array to an array of machine values and return the string
representation (the same sequence of bytes that would be written to a file
by the tofile() method.)

I wonder if this is the method you are looking for. 
So you have an object dbus.Array, which, obviously is from a call to the
dbus (another application's data) that contains 28 byte arrays. I would
assume you know which you want, say the first one.

myDbusString01 = dbus.Array[0].tostring() 

or to get the lot:

myDbusStrings = []  #create a new empty list
for array in dbus.Array:
    myDbusStrings.append( array.tostring() )

At this point you should have the array converted. But you will still need a
reference as to what you have. The call to the dbus should have some
documentation abut what it's returning. 
Also I'd expect the second example to be very bad programming, as some of
the array elements are probably not going to be characters. They could be
integers, floats or booleans. So creating a dictionary to handle specific
array element handling is probably a better, less error prone, method of

Not have the contents and defination of your dbus.array handy, I can't test
this, but the approach seems reasonable.

Steven Howe

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the Python-list mailing list