Take the 2-minute tour ×

I have a char array that i need to convert to hexadecimal value

char* arrrr =[self mountLVparams:NULL :c :code_ward_arr];  

int size =strlen(arrrr);

I am trying with this but this not happening

 NSData* data = [NSData dataWithBytes:arrrr length:sizeof(sizeof(unsigned char)*size)];
share|improve this question
To clarify, do you want to convert to a hexadecimal string like "0xfeed"? –  sbooth Jul 25 '12 at 11:46
no NSData is also fine –  012346 Jul 25 '12 at 11:48
Can you give examples of your desired input and output? I'm still not clear on what you're trying to achieve. –  sbooth Jul 25 '12 at 11:52
see i am using mount method when i return it contains arrrr value that is Emudhra¿services¿¿¿ okay if want that to convert to hexadecimal value –  012346 Jul 25 '12 at 11:57
code is fine check char* arrrr has value –  Prince Jul 25 '12 at 12:00

2 Answers 2

Try this:

NSString *inp = [NSString stringWithString:@"MmMmMm"];
char star = [inp characterAtIndex:3];
NSString *string = [NSString stringWithFormat:@"char:%c andHex:%x",[inp characterAtIndex:3],star];

Look at in output is "%@" as a regular string. In string you will receive hex value of char.

char:m andHex:6d

With array you will have to do it in a loop

share|improve this answer

You don't "convert" a value to hexadecimal. Hexadecimal is just a different way of counting or displaying the same numbers.

if (0xFF==255)
  NSLog(@"255 equals 0xFF");

Now see the following code below which will convert your byte array into and int, then display then format the int as a hex string.

-(void) convertData: (unsigned char*) data 
   int numBytes = sizeof(int);

   //convert array of bytes to NSData*
   NSData* binaryData = [[NSData alloc] initWithBytes: (void*) data length: numBytes];

   //read value of number stored in data, presuming it is an integer
   int number =  *((int*) data);

   //format number as a hex string
   NSString* hexString = [NSString stringWithFormat:@"%X",number];
share|improve this answer

Your Answer


