Objective c 将无符号字符转换为NSString

Objective c 将无符号字符转换为NSString,objective-c,Objective C,因为我对这个问题很陌生,所以这个问题可能看起来很琐碎,所以请不要叫我父母的名字…我有unsigned char和NSString..我想把char值赋给字符串..这是代码段 NSString *passwordDgst = @"somepassword"; unsigned char passwordDgstchar[CC_SHA512_DIGEST_LENGTH]; CC_SHA512(passwordDgst, passwordDgst.length,

因为我对这个问题很陌生,所以这个问题可能看起来很琐碎,所以请不要叫我父母的名字…我有unsigned char和NSString..我想把char值赋给字符串..这是代码段

NSString *passwordDgst = @"somepassword";
unsigned char passwordDgstchar[CC_SHA512_DIGEST_LENGTH];
CC_SHA512(passwordDgst,
          passwordDgst.length,
          passwordDgstchar);

现在,我想将unsigned char passwordDgstchar的值赋给字符串passwordDgst。我如何才能做到这一点?

您在这里遇到了一些问题。该函数接受二进制输入并给出二进制输出。您的代码计算string对象的前12个字节的哈希值。您需要首先将其转换为NSData

NSData* input = [passwordDgst dataUsingEncoding: NSUTF8StringEncoding]; // Could use UTF16 or other if you like
unsigned char passwordDgstchar[CC_SHA512_DIGEST_LENGTH];
CC_SHA512([input bytes],
          [input length],
          passwordDgstchar);
passwordDgstchar现在包含原始二进制文件中的最高位。要将其输入到say hex中,请执行以下操作:

NSMutableString* sha512 = [[NSMutableString alloc] init];
for (int i = 0 ; i < CC_SHA512_DIGEST_LENGTH ; ++i)
{
    [sha512 appendFormat: @"%02x", passwordDgstChar[i];
}

你这里有些问题。该函数接受二进制输入并给出二进制输出。您的代码计算string对象的前12个字节的哈希值。您需要首先将其转换为NSData

NSData* input = [passwordDgst dataUsingEncoding: NSUTF8StringEncoding]; // Could use UTF16 or other if you like
unsigned char passwordDgstchar[CC_SHA512_DIGEST_LENGTH];
CC_SHA512([input bytes],
          [input length],
          passwordDgstchar);
passwordDgstchar现在包含原始二进制文件中的最高位。要将其输入到say hex中,请执行以下操作:

NSMutableString* sha512 = [[NSMutableString alloc] init];
for (int i = 0 ; i < CC_SHA512_DIGEST_LENGTH ; ++i)
{
    [sha512 appendFormat: @"%02x", passwordDgstChar[i];
}

非常感谢Jeremy,但是你能告诉我如何使用base64编码而不是十六进制的值吗?这将是非常有帮助的:没问题..反正我得到了..设计了我自己的函数:谢谢这帮助我将十六进制字符转换为%s的感谢Jeremy,但是你能告诉我如何使用base64编码的值而不是十六进制吗?这将非常有帮助:没问题。反正我得到了。设计了我自己的函数:谢谢这帮助我将十六进制字符转换为%c