ios - How to convert surrogate pair to Unicode scalar in Swift -
the following example taken strings , characters documentation:

the values 55357 (u+d83d in hex) , 56374 (u+dc36 in hex) surrogate pairs form unicode scalar u+1f436, dog face character. there way go other direction? is, can convert surrogate pair scalar?
i tried
let mychar: character = "\u{d83d}\u{dc36}" but got "invalid unicode scalar" error.
this objective c answer , this project seem custom solutions, there built swift (especially swift 2.0+) this?
there formulas calculate original code point based on surrogate pair , vice versa. https://mathiasbynens.be/notes/javascript-encoding#surrogate-formulae:
section 3.7 of unicode standard 3.0 defines algorithms converting , surrogate pairs.
a code point
cgreater0xffffcorresponds surrogate pair<h, l>per following formula:h = math.floor((c - 0x10000) / 0x400) + 0xd800 l = (c - 0x10000) % 0x400 + 0xdc00the reverse mapping, i.e. surrogate pair
<h, l>unicode code pointc, given by:c = (h - 0xd800) * 0x400 + l - 0xdc00 + 0x10000
Comments
Post a Comment