How to convert NSString * to ANTLR3_UINT8 or ANTLR3_UINT16

Can someone tell me what is the correct way to convert NSString * to ANTLR3 string (used in C)?

EDIT: it works

   char buf[255];   
   if ( YES == [myNSString getCString:buf maxLength:255 encoding:NSStringEncodingConversionAllowLossy] )
   {
      pANTLR3_UINT8 theExpression = (pANTLR3_UINT8*)buf;
      ...
+3
source share
1 answer

Assuming you want to get a complete line 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 a different encoding if character loss is not important.

0
source

Source: https://habr.com/ru/post/1742209/


All Articles