NSData 显示为字符串
我正在构建一个 iPhone 应用程序并坚持以下内容:
unsigned char hashedChars[32];
CC_SHA256([inputString UTF8String], [inputString lengthOfBytesUsingEncoding:NSASCIIStringEncoding], hashedChars);
NSData *hashedData = [NSData dataWithBytes:hashedChars length:32];
NSLog(@"hashedData = %@", hashedData);
日志显示如下:
hashedData = <abcdefgh abcdefgh abcdefgh abcdefgh abcdefgh abcdefgh abcdefgh abcdefgh>
- 注意 hashedData 是 NSData,而不是 NSString
但我需要的是将 hashedData 转换为 NSString,如下所示:
NSString *someString = @"abcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefgh";
所以基本上结果需要像 hashedData 除了我不需要尖括号和中间的空格。
I am building an iPhone app and stuck with the following:
unsigned char hashedChars[32];
CC_SHA256([inputString UTF8String], [inputString lengthOfBytesUsingEncoding:NSASCIIStringEncoding], hashedChars);
NSData *hashedData = [NSData dataWithBytes:hashedChars length:32];
NSLog(@"hashedData = %@", hashedData);
The log is showing like:
hashedData = <abcdefgh abcdefgh abcdefgh abcdefgh abcdefgh abcdefgh abcdefgh abcdefgh>
- note hashedData is NSData, not NSString
But what I need is to convert hashedData into NSString that looks like:
NSString *someString = @"abcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefgh";
So basically the result needs to be like hashedData except I don't want the angle brackets and spaces in between.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 NSString initWithData:encoding: 方法。
(编辑以回复您的评论:)
在这种情况下,约书亚的回答确实有帮助:
Use the NSString initWithData:encoding: method.
(edit to respond to your comment:)
In that case, Joshua's answer does help:
我已经找到了解决方案,但我认为我很愚蠢。
基本上我要做的就是:
NSString *someString = [NSString stringWithFormat:@"%@", hashedData];
//forceing theNSData
to intoNSString 。
再次感谢所有试图提供帮助的人,非常感谢
I have found the solution and I think I was being stupid.
Basically all I had to do is:
NSString *someString = [NSString stringWithFormat:@"%@", hashedData];
//forcing theNSData
to becomeNSString
Once again thank you to all who tried to help, Much appreciated.
定义一个 NSCharacterSet 包含有问题的字符,然后使用 -stringByTrimmingCharactersInSet: 过滤字符串。
Define an NSCharacterSet that contains the offending characters then filter your string using -stringByTrimmingCharactersInSet:.