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
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);
}
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);
}
}
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.
What you posted is horribly inefficient. Just search for the bytes.
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;
}
}
}
Should be: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; } } }
for (int x = 0; x < 511 && !foundBytes; x++) {
Should be:
Code:for (int x = 0; x < 511 && !foundBytes; x++) {
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.