如何在iOS中将unsigned char转换为NSString [英] How to convert unsigned char to NSString in iOS

查看:1808
本文介绍了如何在iOS中将unsigned char转换为NSString的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁能告诉我如何将unsigned char转换为NSString?

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

这是我正在使用的代码,但出于某种原因,如果我尝试做任何事情NSString,就像设置UITextView文本一样,它给了我一个错误。虽然NSLog正常工作。提前致谢。

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];

}


推荐答案

如果 tag 是一个C字符串(以null结尾,即),然后你可以使用 [NSString stringWithUTF8String:(char *)tag] 。如果你想要十六进制值,那么使用%02x 的代码就可以了。

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.

这篇关于如何在iOS中将unsigned char转换为NSString的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆