How to convert NSString* to ANTLR3_UINT8 or ANTLR3_UINT16
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;
      ...
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.
 
         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论