[MacRuby-devel] Accessing bytes within NSData
Brian Chapados
chapbr at gmail.com
Tue Mar 24 12:03:32 PDT 2009
Hi Pete,
Since you have an NSData object, you should be able to convert it to a
String (NSString) and then use unpack. For example:
socket_port = NSSocketPort.alloc.initWithTCPPort(11111)
addr = socket_port.address
addr_str = NSString.alloc.initWithData(addr,
encoding:NSString.defaultCStringEncoding)
port = addr_str.unpack("nn")[1] # seems to work - not sure what this should be
I suppose the other alternative would be to add some methods to the
Pointer class making it possible to set the type of a returned
Pointer.
Brian
On Sun, Mar 22, 2009 at 3:18 PM, Pete Yandell <pete at notahat.com> wrote:
> I'd like to use something like Ruby's String#unpack to pull some info out of
> an NSData, but I'm stumped as to how to do it.
>
> NSData.bytes returns a Pointer object, but trying to use [] to dereference
> it gives me:
>
> ArgumentError: can't convert C/Objective-C value `0x800059800' of
> type `?_?' to Ruby object
>
> I've done a lot of digging around, reading of forum posts, playing
> (unsuccessfully) with BridgeSupport, etc., but it hasn't got me any closer.
> Any hints?
>
> The broader context is that I'm trying to extract the TCP port number from
> an NSSocketPort. NSSocketPort's address method returns an NSData, which
> should contain a sockaddr_in, out of which I should be able to pull the
> port.
>
> Thanks,
>
> Pete Yandell
> http://notahat.com/
> _______________________________________________
> MacRuby-devel mailing list
> MacRuby-devel at lists.macosforge.org
> http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
>
More information about the MacRuby-devel
mailing list