如何在 NSMutableData 中设置字节



如何在NSMutableData对象中设置字节?我尝试了以下方法:

-(void)setFirstValue:(Byte)v{
    [mValues mutableBytes][0] = v;
}

但这让编译器大声哭泣...

但这让编译器大声哭泣...

那是因为mutableBytes*返回void* .将其投射到char*以解决问题:

((char*)[mValues mutableBytes])[0] = v;

您也可以使用replaceBytesInRange:withBytes:

char buf[1];
buf[0] = v;
[mValues replaceBytesInRange:NSMakeRange(0, 1) withBytes:buf];

我把它投射到数组

    NSMutableData * rawData = [[NSMutableData alloc] initWithData:data];
    NSMutableArray * networkBuffer = [[NSMutableArray alloc]init];
    const uint8_t *bytes = [self.rawData bytes];
    //cycle through data and place it in the network buffer
    for (int i =0; i < [data length]; i++) 
    {
        [networkBuffer addObject:[NSString stringWithFormat:@"%02X", bytes[i]]];
    }

那么当然,你可以调整你的networkBuffer(这是一个nsmutablearray)中的对象。

最新更新