Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

100
Views
Invalid dog face scalar

I thought I understand Unicode scalars in Swift pretty well, but the dog face emoji proved me wrong.

for code in "🐶".utf16 {
    print(code)
}

The UTF-16 codes are 55357 and 56374. In hex, that's d83d and dc36.

Now:

let dog = "\u{d83d}\u{dc36}"

Instead of getting a string with "🐶", I'm getting an error:

Invalid unicode scalar

I tried with UTF-8 codes and it didn't work neither. Not throwing an error, but returning "ð¶" instead of the dog face.

What is wrong here?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

The \u{nnnn} escape sequence expects a Unicode scalar value, not the UTF-16 representation (with high and low surrogates):

for code in "🐶".unicodeScalars {
    print(String(code.value, radix: 16))
}
// 1f436

let dog = "\u{1F436}"
print(dog) // 🐶

Solutions to reconstruct a string from its UTF-16 representation can be found at Is there a way to create a String from utf16 array in swift?. For example:

let utf16: [UInt16] = [ 0xd83d, 0xdc36 ]
let dog = String(utf16CodeUnits: utf16, count: utf16.count)
print(dog) // 🐶
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!