Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting a uint8_t array to an NSString

How can I affect a uint8_t array (see decryptedBuffer below) to an NSString?

uint8_t *decryptedBuffer;

NSString *cle2=[NSString stringWithUTF8String:decryptedBuffer];

NSString *str2=[player.name AES256DecryptWithKey:cle2]; 
NSLog(str2);


free(plainBuffer);
free(cipherBuffer);
free(decryptedBuffer);
like image 420
chikos Avatar asked Jul 05 '11 07:07

chikos


1 Answers

uint8_t * is just a byte string which is compatible with char *, so you should just be able to pass the casted pointer to stringWithUTF8String, assuming the decrypted string is UTF-8 and it is NULL terminated:

NSString *s = [NSString stringWithUTF8String:(char *)decryptedBuffer];

If the data is not NULL terminated, you can use this:

NSString *s = [[[NSString alloc] initWithBytes:decryptedBuffer
                                 length:length_of_buffer
                                 encoding:NSUTF8StringEncoding] autorelease];
like image 114
Mike Weller Avatar answered Oct 12 '22 21:10

Mike Weller