views:

115

answers:

1

Trying to create an app that does some socket communication (Writing only).

I can get it to work with a static value:

UInt8 buf[] = "play"; int bytesWritten = CFWriteStreamWrite(writeStream, buf, strlen((char*)buf));

I'm a relative neophyte to C/Objective-C and I'm trying to figure out how to pass an NSString to a function and get the value into "buf".

-(void) sendData: (NSString *) command {
    UInt8 buf[] = ????;
    int bytesWritten = CFWriteStreamWrite(writeStream, buf, strlen((char*)buf));
}

Obviously more to it than that...just isolating the relevant code.

+2  A: 

You can access the bytes behind an NSString using the UTF8String method.

CFWriteStreamWrite(writeStream,
                   (const UInt8 *)[command UTF8String],
                   [command lengthOfBytesUsingEncoding:NSUTF8StringEncoding]);
Todd Yandell
Perfect, thank you.
James