Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

mkristain

macrumors regular
Original poster
Aug 18, 2011
115
0
hi can u please tell me how can i find a NSSTring * str=@"xyz";
in unsigned char buffer[50];

thanks.
 
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.
 
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
 
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

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.
 
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.
 
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
 
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?
 
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.

Assuming the char buffer of size 50 is a string and not just a char buffer which might have a string in there somewhere.
 
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
 
Code:
unsigned char byteOne = 0xFF;
unsigned char byteTwo = 0xE0;
int foundBytes = 0;
for([COLOR="Red"]int x;[/COLOR]x < 511 && !foundBytes; x++) {
  if(buffer[x] == byteOne) {
    if(buffer[x+1] == byteTwo) {
      foundBytes = 1;
    }
  }
}
Should be:
Code:
for (int x = 0; x < 511 && !foundBytes; x++) {
 
Last edited:
Coding on my phone rarely ends well. No compiler, try to minimize keystrokes. An altogether suboptimal coding setup.

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.
 
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.
 
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.

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?
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.