how to find a NSString in unsigned char buffer[50]?

Discussion in 'Mac Programming' started by mkristain, Aug 27, 2011.

  1. mkristain macrumors regular

    Joined:
    Aug 18, 2011
    #1
    hi can u please tell me how can i find a NSSTring * str=@"xyz";
    in unsigned char buffer[50];

    thanks.
     
  2. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #2
    What does the buffer contain? A null-terminated string? In what encoding?
     
  3. subsonix macrumors 68040

    Joined:
    Feb 2, 2008
    #3
    If this is a C string you can probably use:

    CFStringGetCStringPtr

    to get a C equivalent to your NSString that you can use in regular C functions.
     
  4. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #4
    I think the real issue is having string data represented two different ways, and wanting to use C string functions or NSString methods to do it. So the next step is to get a C string from an NSString or an NSString from a C string. I'd bet NSString already has methods to handle this. I'd check out the documentation and see.
    http://developer.apple.com/library/...lasses/NSString_Class/Reference/NSString.html

    After that it's either strstr or rangeOfString:.

    -Lee
     
  5. subsonix macrumors 68040

    Joined:
    Feb 2, 2008
    #5
    Maybe so, but an NSString is interchangeable with a CFStringRef and since it's seems likely to be handled in C anyway, might as well do it directly, at least that was my thinking. Looking at the NSString reference there is a method: cStringUsingEncoding that should do the same without having to use Core Foundation.
     
  6. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #6
    Personally I would prefer to "promote" the C string to a NSString and then have the NSString family handle any differences in encoding if necessary. NSStrings -initWithBytesNoCopy:length:encoding:freeWhenDone: can be very cheap if no actual decoding is necessary.

    But if the NSString is constant in a loop where buffer changes a lot, I would "demote" the NSString to a C string before the loop using -cStringUsingEncoding or -UTF8String, which ever is appropriate.
     
  7. mkristain thread starter macrumors regular

    Joined:
    Aug 18, 2011
  8. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #8
    Code:
    NSString *promotedString = [NSString stringWithUTF8String:myCString];
    NSRange whereFound = [promotedString rangeOfString:@"xyz"];
    if(whereFound.location == NSNotFound && whereFound.length == 0) {
      NSLog(@"%@ does not contain xyz.",promotedString);
    } else {
      NSLog(@"%@ contains xyz.",promotedString);
    }
    This is assuming the char *myCString contains your string and it's UTF-8. I haven't compiled this, it was written on my phone. I haven't done this before, it was just cobbled from the doc I referenced. Did you read that document? If so, did you find the relevant methods but didn't know how to apply them?

    -Lee
     
  9. mkristain thread starter macrumors regular

    Joined:
    Aug 18, 2011
    #9
    one more if unsigned char buffer[512]; this have some value and i want to compare the hex of its value to given value like find "FFE0" in unsigned char buffer[512];

    i am using this but its crash after some time.

    Code:
    for(int i=0;i<10000;1;i++)
    {
       int j=0;
      for( j=0;j<512;j++)
      {
    	s1=[s1 stringByAppendingString:[NSString stringWithFormat:@"%02X" , buffer[j]]];
      }
    
      NSRange whereFound = [s1 rangeOfString:@"FFE0"];
      if(whereFound.location == NSNotFound && whereFound.length == 0)
       {
             NSLog(@"%@ does not contain FFE0.",s1);
       } else
       {
      NSLog(@"%@ contains xyz.",s1);
      }
    
    }
    
    so now how can i do this?
     
  10. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #10
    What you posted is horribly inefficient. Just search for the bytes.
     
  11. subsonix macrumors 68040

    Joined:
    Feb 2, 2008
    #11
    Assuming the char buffer of size 50 is a string and not just a char buffer which might have a string in there somewhere.
     
  12. mkristain thread starter macrumors regular

    Joined:
    Aug 18, 2011
    #12
    please help me how ?
     
  13. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #13
    Code:
    unsigned char byteOne = 0xFF;
    unsigned char byteTwo = 0xE0;
    int foundBytes = 0;
    for(int x;x < 511 && !foundBytes; x++) {
      if(buffer[x] == byteOne) {
        if(buffer[x+1] == byteTwo) {
          foundBytes = 1;
        }
      }
    }
    
    I don't have any idea what your outer loop is doing. I do know that you create 5,120,000 NSStrings with an average length of 1,024 bytes. This is a total of 5,242,880,000 bytes without even considering the overhead of the objects. If you're on a 32-bit system this exceeds the virtual memory size your app gets, 4GB. This is probably why you crash.

    -Lee
     
  14. chown33, Aug 29, 2011
    Last edited: Aug 29, 2011

    chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #14
    Should be:
    Code:
    for (int x = 0; x < 511 && !foundBytes; x++) {
    
     
  15. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #15
    Indeed, thanks. Coding on my phone rarely ends well. No compiler, try to minimize keystrokes. An altogether suboptimal coding setup.

    -Lee
     
  16. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #16
    It's a shame there's not a C-compiler web-service, nor a C syntax-checker in a pastebin-like service, nor even a C-compiler implemented in JavaScript. Though now that I say it, I suppose someone will google it and find dozens of them.
     
  17. Catfish_Man macrumors 68030

    Catfish_Man

    Joined:
    Sep 13, 2001
    Location:
    Portland, OR
    #17
    Here you go: http://llvm.org/demo/
     
  18. mkristain thread starter macrumors regular

    Joined:
    Aug 18, 2011
    #18
    ok .

    i have "FFE0" is in string so first i need to convert in unsigned char than compare with buffer value.

    is this right or efficient please guide me.
     
  19. lee1210, Aug 29, 2011
    Last edited: Aug 29, 2011

    lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #19
    I posted code in this thread, which required a tweak, but otherwise should do the job. In the duplicate thread you posted somone gave a pretty much equivalent solution. You only have that in a string because you chose to. Your real task is looking for those values treated as hex in your list of bytes. Solutions that are pretty efficient were already posted, including an explanation of why your solution was not efficient.

    -Lee

    Edit: are you having trouble understanding the code that's been posted in both threads? If so, what specifically doesn't make sense?
     
  20. mkristain thread starter macrumors regular

    Joined:
    Aug 18, 2011

Share This Page