开发者

How to convert NSString* to ANTLR3_UINT8 or ANTLR3_UINT16

开发者 https://www.devze.com 2022-12-27 22:45 出处:网络
Could somebody tell me what is the proper way to convert a NSString* to an ANTLR3 string (used in C) ?

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号