Domanda

I create random file:

-(NSData*)generateRandomNSDataWithNumberOfRecords:(int)length {

NSMutableData* theData = [[NSMutableData alloc] init];
int numberOfElements;

for (int i=0; i<length; i++) {
    numberOfElements = arc4random() % 15;
    if (numberOfElements == 0)
        numberOfElements = 1;

    NSData *tempData = [NSData dataWithBytes: &numberOfElements length: sizeof(numberOfElements)];
    [theData appendData:tempData];
    for(int j = 0 ; j < numberOfElements; j++)
    {

        int randomBits = arc4random()%255; // 0 to 255
        [theData appendData:[NSData dataWithBytes: &randomBits length: sizeof(randomBits)]];
    }

}
return theData;

}

and when hexdumping it i can see:

hexdump -C Tape1

00000000  04 00 00 00 cd 00 00 00  14 00 00 00 65 00 00 00  |............e...|
00000010  16 00 00 00 0a 00 00 00  d2 00 00 00 c1 00 00 00  |................|
00000020  02 00 00 00 f6 00 00 00  e4 00 00 00 ce 00 00 00  |................|

First one should be 4 (Checked out over debugger). but it appear to be written other way round.

Is there a simple solution to tell NSData to store data "properly"?

È stato utile?

Soluzione

It's endian issue.

I just needed to:randomBits = CFSwapInt32HostToBig(randomBits);

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top