1

I have an NSString like this @"uE604" that I want to add a backslash too in order to use it as a unicode character. However I am having issues trying to get that to work. If I add @"\\" to the front of it, it doesn't work.

Any way I can get it working?

Jargen89
  • 480
  • 1
  • 6
  • 19

2 Answers2

1

Try using just one slash: @"\uE604".

par
  • 17,361
  • 4
  • 65
  • 80
  • how would I concatenate that string to `@"\"`? – Jargen89 Oct 02 '15 at 15:02
  • The sequence \uXXXX is a unicode string literal. The hex sequence is decoded at compile-time and the actual unicode symbol is inserted in its place. Is this when you want to put the character in, or do you want to do it with some arbitrary hex string at runtime? – par Oct 02 '15 at 15:07
  • If you want a backlash in front of a unicode character at compile-time, you would do `@"\\\uE604"`. – par Oct 02 '15 at 15:10
  • Okay, so I would have to try and convert the initial text into a hex string and then concatenate that to 5c, then do another conversion into the unicode character? – Jargen89 Oct 02 '15 at 15:11
  • 1
    If you want to do it at runtime, yes. First convert the hex string to an integer (see http://stackoverflow.com/a/3648562/312594), then initialize an NSString with its value (see http://stackoverflow.com/a/30817609/312594). – par Oct 02 '15 at 15:14
  • The backslash is an *escape sequence* and tells the compiler to do something special with the text that follows it. If you're converting a hex string to an integer then to a unicode character at runtime there is no need for the backslash to be involved at all (unless you specifically want to print it out for some reason, but that doesn't seem like what you need here). – par Oct 02 '15 at 15:17
  • I'm just trying to construct the unicode character based on the string I'm provided. I believed that the backslash is necessary. – Jargen89 Oct 02 '15 at 15:22
0

Just use one slash, since you are working with Unicode you don't need to escape the slash:

NSString *s = @"\u266A";
Caio
  • 371
  • 5
  • 11