Reputation: 6172
i tried hard to solve this one on my own but ive spent way to much time on something as simple as this so im hoping someone can give me the answer.
see image below but basically im just trying to get a range built but my marker and my list never meet even though there are items that match the criteria.
in the example below im looking for 2c and the array has it in position 11 but just goes right past it.
i have tried a few different ways, and the comparison above it works just fine so i dont get where my problem is. i have tried just saying
== 0x2c
(never evaluates)
or using this like i had above
`intValue`
but this make it get a match on 2D which from my testing is because it truncates the digits down to just plain 2.
ACTUAL CODE:
int lastIndexInPayload = ([payload count]-1);
int beginningOfData = ([payload count]-1);
NSString * endOfLine = [NSString stringWithFormat:@"%02X", 0x0D];
NSString * startOfLine = [NSString stringWithFormat:@"%02X", 0x2C];
if ([[payload objectAtIndex:lastIndexInPayload]intValue] == [endOfLine intValue]) {
while ([payload objectAtIndex:beginningOfData]!= startOfLine) {
beginningOfData--;
DDLogVerbose(@"beginningOfData %d",beginningOfData);
}
}
the payload is an array of bytes taken from nsdata from a socket return
Upvotes: 0
Views: 113
Reputation: 57169
You are comparing strings according to their address and not their contents, you should use while (![[payload objectAtIndex:beginningOfData] isEqualToString:startOfLine])
. Also why are you trying to use strings for hex values and not just compare against 0xD
directly? Your call to intValue will result in 0
for endOfLine because it does not know that it is hex and will stop trying to parse a number.
Upvotes: 1
Reputation: 185671
I imagine it's because you're using ==
to compare objects. Since it appears both your array and your startOfLine
object are just hex strings representing a single byte, you should probably just use -isEqualToString:
, as in
while (![[payload objectAtIndex:beginningOfData] isEqualToString:startOfLine]) {
That said, I find it rather odd that your payload is an array of hex bytes represented as strings. Why not just shove the whole thing into an NSData and have access to "real" bytes instead?
Upvotes: 1