Re: [Pyobjc-dev] Reading binary data
Brought to you by:
ronaldoussoren
From: Bob I. <bo...@re...> - 2006-03-08 17:41:10
|
On Mar 8, 2006, at 8:51 AM, Barry Wark wrote: > On 3/8/06, Brian O'Brien <bo...@uc...> wrote: >> >> On 8-Mar-06, at 12:02 AM, Ronald Oussoren wrote: >> >>> >>> On 8-mrt-2006, at 6:50, Brian O'Brien wrote: >>> >>>> Hi in python I'm able to read in binary data from a file. >>>> However the data is 16bits per sample and python is storing the >>>> data in a string. How do I convert that 8bit data into a list of >>>> 16 bit integers? >>> >>> This is not really a pyobjc related question, but you could use >>> array.array. >>> >>> a = array.array('H') >>> a.fromstring(binarydata) >>> >>> Ronald >> Ok now we get to the pyobjc related question.... >> Now that the data is in an array object and I want to pass it on to >> objective C... >> What does objective c receive as a type? >> When I was passing a=[1,2,3] objectiveC was happy receiving the >> list as >> an NSArray. I've been told that the str object is received as >> NSData. > > If you're using numpy, you can call arr.tolist() to convert your array > to a Python list, which PyObjC will bridge as an NSArray to > Objective-C. If you'd rather have the array as an NSData, you can use > NSData.dataWithData_(arr.data), at least in numpy. Similar > functionality exists in numarray, but I don't know it off the top of > my head. > > On large arrays, tolist() can be pretty inefficient. Maybe some of the > PyObjc gurus could comment on what it would take to bridge numpy > arrays (maybe just 1-D arrays?) directly to NSArrays. Numeric and numarray arrays support the buffer API so they will automatically get (zero copy) bridged to NSData if you pass them as-is. -bob |