Mr Aleph
Mr Aleph

Reputation: 1895

Convert wchar_t to CFStringref

I have a wchar_t * that I need to use on a function that needs a CFStringref

I tried using CFStringCreateWithCharacters but I'm not getting anywhere with it.

So, if I have:

wchar_t * widecharvar = L"some value";
CFStringRef stringref;

How do I convert and copy widecharvar to stringref? This value will be used in SecRequirementCreateWithString()

Thanks

Upvotes: 4

Views: 1787

Answers (1)

Rob Napier
Rob Napier

Reputation: 299345

I'm doing this off the top of my head and memory; if it has trouble, comment and I'll actually test it.

// Check our byte order. Assuming we made the string as in your example
CFStringEncoding encoding = (CFByteOrderLittleEndian == CFByteOrderGetCurrent()) ? 
                              kCFStringEncodingUTF32LE : kCFStringEncodingUTF32BE;

int widecharvarLen = wcslen(widecharvar); 

CFStringRef string = CFStringCreateWithBytes(NULL, 
                                             widecharvar, 
                                             (widecharvarLen * sizeof(wchar_t)),  
                                             encoding,
                                             false);

That last false means that string does not include a BOM (Byte Order Mark), which is the kind of string I assume you're dealing with.

Upvotes: 5

Related Questions