NSMutableData到const uint8

时间:2013-01-23 07:53:53

标签: objective-c

我有一个奇怪的问题

服务器发送功能

-(void) writeToServer:(const uint8_t *) buf {
    NSLog(@"sending buf %s with lenght %zd", buf, strlen((char*)buf));
    [oStream write:buf maxLength:strlen((char*)buf)]; 
     // NSOutputStream *oStream
}

buf

NSLog(@"converted %@ length %d", encrypted, [encrypted length]);
// encrypted is NSMutableData
return (const uint8_t*)[encrypted bytes]; 

输出

 converted <4b010000 00000006 00000000 00000f20 30303031 4e50> length 22
 sending buf K with lenght 2

我需要buf保持不变..使用相同的原始字节和适当的长度,我应该怎么做?

1 个答案:

答案 0 :(得分:1)

直接传递NSData对象:

-(void)writeToServer:(NSData *)data
{
    NSLog(@"sending with length %lu", (unsigned long)[data length]);
    [oStream write:[data bytes] maxLength:[data length]]; 
}