我正在构建一个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一样,除了我不希望尖括号和两者之间有空格。

    最佳答案

    使用NSString initWithData:encoding:方法。

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

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

    在这种情况下,约书亚的答案确实有帮助:
    NSCharacterSet *charsToRemove = [NSCharacterSet characterSetWithCharactersInString:@"< >"];
    NSString *someString = [[hashedData description] stringByTrimmingCharactersInSet:charsToRemove];
    

    关于iphone - NSData显示为字符串,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2653288/

    10-08 23:04