Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert unsigned char to NSString in iOS

Tags:

iphone

ios4

Can anyone tell me how to convert an unsigned char to an NSString?

Here's the code I am using, but for some reason if I try to do anything with the NSString, like set a UITextView text, it gives me an error. The NSLog works correctly though. Thanks in advance.

- (void)onTagReceived:(unsigned char *)tag
{

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSString *myTag = [NSString stringWithFormat:@"%02x%02x%02x%02x%02x\n",tag[0],tag[1],tag[2],tag[3],tag[4]];

NSLog(@"currentTag: %@",myTag);

[displayTxt setText:myTag];

[pool release];

}
like image 718
dreadbot Avatar asked Jun 26 '11 18:06

dreadbot


1 Answers

If tag is a C string (null-terminated, that is), then you can use [NSString stringWithUTF8String:(char *)tag]. If you want the hex values, then your code using %02x is fine.

like image 61
jtbandes Avatar answered Sep 21 '22 06:09

jtbandes