5

Trying to compare to a string: BOOL r = [res isEqualToString:[@"\x124Vx\xc3\xaa"]];

However I got

error: hex escape sequence out of range

I tried also something like:

NSString *s = [@"\x12" stringByAppendingString: @"4Vx\xc3\xaa"];
BOOL r = [res isEqualToString:s];

Now it could work and return YES

How can I specify such string and avoiding splitting them up first? It's kind of annoying...

UPDATE: I use @rmaddy's code, and it works now, however, If I put it in array like:

NSArray *answers = @[
                     @"",
                     @"#",
                     @"\x12""4Vx\xc3\xaa",
                     ] 

It will generate a warning for the last string literal:

Concatenated NSString literal for an NSArray expression - possibly missing a comma How to get rid of it?

Using

NSString *s = @"\x12""4Vx\xc3\xaa";
NSArray *arr = @[s];

Can work, but I prefer not writing more NSString *s ... to do it.

Wingzero
  • 9,644
  • 10
  • 39
  • 80
  • 1
    Related: http://stackoverflow.com/questions/5784969/when-did-c-compilers-start-considering-more-than-two-hex-digits-in-string-lite. – Martin R Nov 30 '16 at 21:00

1 Answers1

6

The problem with:

BOOL r = [res isEqualToString:@"\x124Vx\xc3\xaa"];

is with the \x124 part. It seems you can only have hex values in the range 00 - ff. And note the removal of the [ ] around the string.

If you don't want the 4 to be considered part of the \x hex number, you can do this:

BOOL r = [res isEqualToString:@"\x12""4Vx\xc3\xaa"];

The two double-quote characters ensure the \x escape sequence stops where you need it to.

To eliminate the new warning about a possible missing comma when using such a string in an NSArray, you will need to use the older syntax to create the array:

NSArray *answers = [NSArray arrayWithObjects:
                     @"",
                     @"#",
                     @"\x12""4Vx\xc3\xaa",
                     nil
                     ];
rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • Thanks! Your code did the job, but it also comes with a warning:`Concatenated NSString literal for an NSArray expression - possibly missing a comma`, is there anyway to get rid of it? – Wingzero Nov 29 '16 at 03:35
  • ok, I found the reason, I put the string literal in an array, so it gives this warning. like `@[@"1", @"\x12""4Vx\xc3\xaa"]` – Wingzero Nov 29 '16 at 03:37
  • I actually didn't notice the `[ ]` around the string. Yeah, that needs to go. – rmaddy Nov 29 '16 at 03:37
  • do you know why putting it in array will cause the warning? – Wingzero Nov 29 '16 at 03:38
  • It can't be in an array. The `isEqualToString:` method expects a string, not an array. Just remove the extra square brackets and it will be fine. – rmaddy Nov 29 '16 at 03:38
  • No you misunderstand. Actually I am writing unit tests for comparing, so I put the strings into an array, and fetch them to compare. If I put the string literal in the array, it will give the warning. – Wingzero Nov 29 '16 at 03:39
  • Hi Rmaddy, I tried add `nil`, but it directly report error:`Collection element of type 'void *' is not an Objective-C object` – Wingzero Nov 29 '16 at 04:19
  • The code I put in the answer compiles clean for me. You must have a typo. – rmaddy Nov 29 '16 at 04:21
  • Turned out I am using array literal, not API calls.. Thanks. – Wingzero Nov 29 '16 at 04:46