Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 498
Makleesh Avatar asked Oct 17 '12 07:10

Makleesh


2 Answers

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];
like image 69
zoul Avatar answered Sep 19 '22 13:09

zoul


Here's how I've done that before:

UInt8 j= 0x0f;
NSData *data = [[[NSData alloc] initWithBytes:&j length:sizeof(j)] autorelease];
like image 43
Tommy Devoy Avatar answered Sep 18 '22 13:09

Tommy Devoy