NSData 显示为字符串

发布于 2024-08-29 07:34:55 字数 714 浏览 4 评论 0原文

我正在构建一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

在你怀里撒娇 2024-09-05 07:34:55

使用 NSString initWithData:encoding: 方法。

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

(编辑以回复您的评论:)

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

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

Use the NSString initWithData:encoding: method.

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

(edit to respond to your comment:)

In that case, Joshua's answer does help:

NSCharacterSet *charsToRemove = [NSCharacterSet characterSetWithCharactersInString:@"< >"];
NSString *someString = [[hashedData description] stringByTrimmingCharactersInSet:charsToRemove];
莫相离 2024-09-05 07:34:55

我已经找到了解决方案,但我认为我很愚蠢。

基本上我要做的就是:

NSString *someString = [NSString stringWithFormat:@"%@", hashedData]; //forceing the NSData to into NSString 。

再次感谢所有试图提供帮助的人,非常感谢

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.

苍景流年 2024-09-05 07:34:55

定义一个 NSCharacterSet 包含有问题的字符,然后使用 -stringByTrimmingCharactersInSet: 过滤字符串。

Define an NSCharacterSet that contains the offending characters then filter your string using -stringByTrimmingCharactersInSet:.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文