0

For example, we have:

var char:Character="\u{1F496}"   //get a Character like a heart

How to do the opposite work, like how to get Character ("A")'s unicode.

Allen
  • 354
  • 1
  • 3
  • 8
  • 5
    Do you think this can help? http://stackoverflow.com/questions/24102044/how-can-i-get-the-unicode-code-points-of-a-character – Picrofo Software Aug 16 '14 at 02:41
  • Try casting it to an integer, I don't know how that works in Swift but [here is an article about casting in swift](https://developer.apple.com/library/prerelease/mac/documentation/Swift/Conceptual/Swift_Programming_Language/TypeCasting.html). This should give you the data in UTF-16, which should be useful to you. – john Aug 16 '14 at 03:00
  • @PicrofoSoftware Yes, what else should I do? Delete it? – Allen Aug 16 '14 at 08:13

2 Answers2

1

Can do something like that:

let char: Character = "\u{1F496}"
let value: Int = Int(NSString(string: String(chat)).characterAtIndex(0))
Vasyl Khmil
  • 2,548
  • 1
  • 20
  • 36
  • 'let dogString = "" for codeUnit in dogString.utf16 { print("\(codeUnit) ") }' //it gives 55357 and 56374, while your code gives 55357, how to do the opposite work? From (55357, 56374) to "" – Allen Aug 16 '14 at 08:18
  • @Allen Use these formulas to convert between Unicode code points and UTF-16 surrogate halves: https://mathiasbynens.be/notes/javascript-encoding#surrogate-formulae – Mathias Bynens Aug 17 '14 at 08:14
  • Thank you. And this helps (http://segmentfault.com/q/1010000000638151) – Allen Aug 20 '14 at 11:31
0

Character does not represent a single Unicode code point, but rather a "Unicode grapheme cluster". To represent a single Unicode code point, you want UnicodeScalar.

let char: UnicodeScalar = "\u{1F496}"
println(char.value) // prints "128150"
newacct
  • 119,665
  • 29
  • 163
  • 224