views:

95

answers:

1

Could somebody tell me what is the proper way to convert a NSString* to an ANTLR3 string (used in C) ?

EDIT: this seems to work

   char buf[255];   
   if ( YES == [myNSString getCString:buf maxLength:255 encoding:NSStringEncodingConversionAllowLossy] )
   {
      pANTLR3_UINT8 theExpression = (pANTLR3_UINT8*)buf;
      ...
A: 

Assuming that you want the complete string without loss, you can use the following code:

NSString *myString = ...;
NSUInteger len = [myString lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
char *buf = malloc(len + 1);
if ([myString getCString:buf maxLength:(len + 1) encoding:NSUTF8StringEncoding]) {
    // pANTLR3_UINT8 is equivalent to ANTLR3_UINT8*
    pANTLR3_UINT8 theExpression = (pANTLR3_UINT8) buf;
}
free(buf);

Of course you can use another encoding if character loss is not important.

Laurent Etiemble
after some experimenting I got about the same, thank you nevertheless.
Anders K.