转换一个CFStringRef为char

转换一个CFStringRef为char

本文介绍了转换一个CFStringRef为char *的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到一个 CFStringRef 出的 CFDictionaryRef 使用 CFDictionaryGetValue

我一直在试图将 CFStringRef 转换为的char * 使用 CFStringGetCString CFStringGetCStringPtr ,他们要么返回null或崩溃。

有没有办法做到这一点?怎么样?

感谢您。

编辑:样品code:

  SecStatic codeREF静态code;
CFDictionaryRef信息;
SecCSFlags标志= kSecCSInternalInformation
            | kSecCSSigningInformation
            | kSecCSRequirementInformation
            | kSecCSInternalInformation;
CFURLRef pathURL = NULL;
CFStringRef pathStr = NULL;
CFStringRef UNIQUEID;
字符*海峡= NULL;
CFIndex长度;
pathStr = CFStringCreateWithCString(kCFAllocatorDefault,
                                    文件名,kCFStringEncodingUTF8);
pathURL = CFURLCreateWithString(kCFAllocatorDefault,pathStr,NULL);
SecStatic codeCreateWithPath(pathURL,kSecCSDefaultFlags,&安培;静态code);
秒codeCopySigningInformation(静态code,标志和安培;信息);UNIQUEID =(CFStringRef)CFDictionaryGetValue(信息,昆船codeInfoUnique);//我怎么在这里将其转换为char *?
长度= CFStringGetLength(UNIQUEID);
海峡=(字符*)malloc的(长+ 1);
CFStringGetCString(UNIQUEID,STR,长度,kCFStringEncodingUTF8);的printf(签名哈希为%s \\ n,STR);CFRelease(信息);
CFRelease(静态code);


解决方案

从第17章在

 的char * MYCFStringCopyUTF8String(CFStringRef ASTRING){
  如果(ASTRING == NULL){
    返回NULL;
  }  CFIndex长度= CFStringGetLength(ASTRING);
  CFIndex MAXSIZE =
  CFStringGetMaximumSizeForEncoding(长度,kCFStringEncodingUTF8)+ 1;
  字符*缓冲=(字符*)malloc的(MAXSIZE);
  如果(CFStringGetCString(ASTRING,缓冲,MAXSIZE,
                         kCFStringEncodingUTF8)){
    返回缓冲区;
  }
  返回NULL;
}

生成的缓冲区必须始终被释放(这就是为什么复制是名称)。链接的例子code也有一个使用你提供缓冲稍快的版本。

I am getting a CFStringRef out of a CFDictionaryRef using CFDictionaryGetValue.

I've been trying to convert the CFStringRef to a char* using CFStringGetCString or CFStringGetCStringPtr and they either return a NULL or it crashes.

Is there a way to do this? How?

Thank you.

EDIT: sample code:

SecStaticCodeRef staticCode;
CFDictionaryRef information;
SecCSFlags flags = kSecCSInternalInformation
            | kSecCSSigningInformation
            | kSecCSRequirementInformation
            | kSecCSInternalInformation;
CFURLRef pathURL = NULL;
CFStringRef pathStr = NULL;
CFStringRef uniqueid;
char* str = NULL;
CFIndex length;


pathStr = CFStringCreateWithCString(kCFAllocatorDefault,
                                    filename, kCFStringEncodingUTF8);
pathURL = CFURLCreateWithString(kCFAllocatorDefault, pathStr, NULL);
SecStaticCodeCreateWithPath(pathURL, kSecCSDefaultFlags, &staticCode);
SecCodeCopySigningInformation(staticCode, flags, &information);

uniqueid = (CFStringRef) CFDictionaryGetValue(information, kSecCodeInfoUnique);

// how do I convert it here to char *?
length = CFStringGetLength(uniqueid);
str = (char *)malloc( length + 1 );
CFStringGetCString(uniqueid, str, length, kCFStringEncodingUTF8);

printf("hash of signature is %s\n", str);

CFRelease(information);
CFRelease(staticCode);
解决方案

From the Chapter 17 example code in iOS:PTL.

char * MYCFStringCopyUTF8String(CFStringRef aString) {
  if (aString == NULL) {
    return NULL;
  }

  CFIndex length = CFStringGetLength(aString);
  CFIndex maxSize =
  CFStringGetMaximumSizeForEncoding(length, kCFStringEncodingUTF8) + 1;
  char *buffer = (char *)malloc(maxSize);
  if (CFStringGetCString(aString, buffer, maxSize,
                         kCFStringEncodingUTF8)) {
    return buffer;
  }
  return NULL;
}

The resulting buffer must always be freed (which is why Copy is in the name). The linked example code also has a slightly faster version that uses a buffer you provide.

这篇关于转换一个CFStringRef为char *的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 11:27