Accessing bytes within NSData
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/
Did I miss the answer to this one? I have a similar issue returning bytes, voids, GLfloats etc J On Mar 22, 2009, at 11:18 PM, Pete Yandell 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@lists.macosforge.org http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
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@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@lists.macosforge.org http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
On 25/03/2009, at 6:03 AM, Brian Chapados wrote:
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
Thanks, Brian. That was just the ticket. Not sure why I didn't think of it! - Pete
participants (3)
-
Brian Chapados
-
John Shea
-
Pete Yandell