Makleesh
Makleesh

Reputation: 988

Store hex value into NSData

I'm confused on how to store a hex value into NSData. I want the value I will be storing to be 0x0F.

Please could someone give me an example?

Upvotes: 11

Views: 9977

Answers (2)

zoul
zoul

Reputation: 104065

You can create an array to hold your hex values and then use that to create the data:

unsigned char bytes[] = {0x0F};
NSData *data = [NSData dataWithBytes:bytes length:1];

This can be rewritten as:

NSData *data = [NSData dataWithBytes:(unsigned char[]){0x0F} length:1];

Upvotes: 20

Tommy Devoy
Tommy Devoy

Reputation: 13549

Here's how I've done that before:

UInt8 j= 0x0f;
NSData *data = [[[NSData alloc] initWithBytes:&j length:sizeof(j)] autorelease];

Upvotes: 11

Related Questions