NSData显示为字符串(NSData to display as a string)

系统教程 行业动态 更新时间:2024-06-14 17:03:52
NSData显示为字符串(NSData to display as a string)

这是我的第一篇文章。 我正在构建一个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.

最满意答案

使用NSString initWithData:encoding:方法。

NSString *someString = [[NSString alloc] initWithData:hashedData encoding:NSASCIIStringEncoding];

(编辑回复你的评论:)

在这种情况下,约书亚的回答确实有帮助:

NSCharacterSet *charsToRemove = [NSCharacterSet characterSetWithCharactersInString:@"< >"]; NSString *someString = [[hashedData description] stringByTrimmingCharactersInSet:charsToRemove];

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 the NSData to become NSString

Once again thank you to all who tried to help, Much appreciated.

更多推荐

本文发布于:2023-04-24 12:16:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/999e8ff28e2d75bd8cc2db530f8a318f.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   NSData   string   display

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!