Byte-Array to String

Robert Rawlins - Think Blue robert.rawlins at thinkbluemedia.co.uk
Fri Apr 20 09:39:36 EDT 2007


Thanks for that Carsten,

I've given that a go and I still get similar results to what I've seen in the past whereby it prints a couple of elements I would expect to see but other which I'm not sure about, and what seems to be ALOT of blank characters.

6e
                555     5       5       5               en      j                       5               %OBEX Object Push       ÿ

That's what your suggestion returns to me, I'm using the API to scan a Bluetooth device for services, one of which is OBEX Object Push, but the rest of that output is apparent junk.

Unfortunately the API doesn’t really give any suggestions at all, take a look here http://bluez.cvs.sourceforge.net/*checkout*/bluez/utils/hcid/dbus-api.txt if you look down under the 'adapter' section, I'm running the method called GetRemoteServiceRecord() which returns the byte array but there really isn’t any suggestion as to how you extract the information from what's returned.

I have no idea if we're barking up the right tree here or if we're going about this the wrong way.

Thanks again for all your helps guys,

Rob

-----Original Message-----
From: Carsten Haese [mailto:carsten at uniqsys.com] 
Sent: 20 April 2007 14:00
To: Robert Rawlins - Think Blue
Cc: python-list at python.org
Subject: RE: Byte-Array to String

On Fri, 2007-04-20 at 09:51 +0100, Robert Rawlins - Think Blue wrote:
> Morning Steve,
> 
>  
> 
> That stuff looks mighty promising, I did play around with the
> toString() function yesterday but couldn’t get the damned thing
> working.

That's because Steven seems to have given you suboptimal advice. The
thing you're working with is a dbus.Array of dbus.Byte, not a Python
array, so Python's standard array module is not going to help you.

When you work with an API function and you don't know what to do with
the results of calling that function, the documentation for that API
should be your first place to look for help. I don't know what you're
referencing, but Google tells me that the python-dbus API is documented
here: http://dbus.freedesktop.org/doc/dbus-python/api/

So, what do you do with a dbus.Array full of instances of dbus.Byte?
Let's look at their documentation,
http://dbus.freedesktop.org/doc/dbus-python/api/dbus.Array-class.html
and
http://dbus.freedesktop.org/doc/dbus-python/api/dbus.Byte-class.html,
respectively.

The page about dbus.Array tells us that it inherits from list, so you
should be able to do indexed access into it and iterate over it.
dbus.Byte inherits from int, and the documentation says that a dbus.Byte
can be converted to a character (string of length 1) by calling str or
chr on it.

So, to get a string from your dbus.Array, you could do something like
this:

s = ""
for b in myDbusArray:
  s += chr(b)

However, it's better (faster) to use the join idiom for building a
string char-by-char:

s = "".join(chr(b) for b in myDbusArray)

That ought to give you the string you're asking for.

HTH,

Carsten.






More information about the Python-list mailing list