Converting NSString to PA_Unistring

Hello,

I need to convert NSStrings to PA_Strings. Can anyone point me to the right direction?

Stefan Hanshans

PA_String is really old (pre-4D v11) - text is stored as Pascal string using Macintosh characters…
Something like this should do it:
Code :
void CreatePAStringFromNSString (NSString* pString, PA_String *paString)
{
char *p = paString->fString + 1;
paString->fSize = 255;
*p = ‘\0’;
[pString getCString:p maxLength:255 encoding: NSMacOSRomanStringEncoding];
paString->fString[0] = strlen §;
}

To get PA_Unistring, try something like this:
Code :
PA_Unistring CreatePAUnistringFromNSString (NSString* pString)
{
uint32_t len = [pString length];
uint32_t size = (len * sizeof(PA_Unichar)) + sizeof(PA_Unichar);
std::vector<uint8_t> buf(size);
PA_Unistring uniString;
if([pString getCString:(char *)&buf[0] maxLength:size encoding:NSUnicodeStringEncoding])
uniString = PA_CreateUnistring ((const PA_Unichar *)&buf[0]);
else
uniString = PA_CreateUnistring ((const PA_Unichar *)"\0\0");
return uniString;
}

Both snippets not tested, written in browser.

Thank you, this is exactly what I was looking for. I had to make a few changes and now it is running:

Code :
PA_Unistring CreatePAUnistringFromNSString (NSString* pString)
{
uint32_t len = (uint32_t) [pString length];
uint32_t size = (len * sizeof(PA_Unichar)) + sizeof(PA_Unichar);
std::vector<PA_Unichar>buf(size);
PA_Unistring uniString;
if([pString getCString:(char *)&buf[0] maxLength:size encoding:NSUnicodeStringEncoding])
uniString = PA_CreateUnistring ((PA_Unichar *)&buf[0]);
else
uniString = PA_CreateUnistring ((PA_Unichar *)"\0\0");
return uniString;
}