RE: Byte-Array to String

Discussion in 'Python' started by Robert Rawlins - Think Blue, Apr 20, 2007.

  1. 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:]
    Sent: 20 April 2007 14:00
    To: Robert Rawlins - Think Blue
    Cc:
    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.
     
    Robert Rawlins - Think Blue, Apr 20, 2007
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bharat Bhushan

    Appending byte[] to another byte[] array

    Bharat Bhushan, Aug 5, 2003, in forum: Java
    Replies:
    15
    Views:
    40,406
    Roedy Green
    Aug 5, 2003
  2. Kirby
    Replies:
    3
    Views:
    669
    Kirby
    Oct 8, 2004
  3. Replies:
    20
    Views:
    9,904
    licebmi
    Sep 7, 2009
  4. Tom McGlynn
    Replies:
    4
    Views:
    878
    Mark Space
    Apr 19, 2008
  5. Patricia Shanahan
    Replies:
    0
    Views:
    408
    Patricia Shanahan
    Apr 17, 2008
Loading...

Share This Page