fetzig
fetzig

Reputation: 1663

reverse order of bytes in NSData

never did this (except school). so i'm totally on the dark.

i have data (void *) and it's size (UInt32) so i can simply make something like

[NSData dataWithBytesNoCopy:data length:dataSize freeWhenDone:YES]

this to get a NSData instance (which i need), but i want "slice" the this data in 1 or 2 byte blocks and reverse it's order.

a short example would be great.

thx.

Upvotes: 0

Views: 1585

Answers (2)

Fattie
Fattie

Reputation: 12272

if you're struggling to find out what the heck is going on in the bytes during development,

typedef struct _SliceItUp
    {
    UInt8   a,b,c,d; // 4 x 8bits == 32 bits
    }
    SliceItUp;

SliceItUp dd;

[data getBytes:&dd length:sizeof(SliceItUp)];

NSLog(@"the four friends are %d %d %d %d", dd.a,dd.b,dd.c,dd.d);

You now have the four bytes individually as four totally distinct bytes.

You have total control and you can do whatever you want.

Upvotes: 6

NSResponder
NSResponder

Reputation: 16861

Check the documentation for the functions defined in NSByteOrder.h

Upvotes: 1

Related Questions