ios - How to convert surrogate pair to Unicode scalar in Swift -


the following example taken strings , characters documentation:

enter image description here

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 c greater 0xffff corresponds surrogate pair <h, l> per following formula:

h = math.floor((c - 0x10000) / 0x400) + 0xd800 l = (c - 0x10000) % 0x400 + 0xdc00 

the reverse mapping, i.e. surrogate pair <h, l> unicode code point c, given by:

c = (h - 0xd800) * 0x400 + l - 0xdc00 + 0x10000 

Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -