0

I want to print Unicode characters for some special symbol like `,',@,$ so please let me know if there is any method which converts string to unicode characters like \u00e2...

Thanks,

Rahul gupta
  • 129
  • 8
  • Refer this link https://stackoverflow.com/questions/31696142/print-unicode-character-from-variable-swift – Saurabh Aug 30 '18 at 08:08
  • Is it your goal to access the characters of a string, or, given a string of unicode characters, to get their unicode descriptors, e.g. given `"â"` receive `"\u00e2"`? – waldrumpus Aug 30 '18 at 08:49

1 Answers1

1

You can get your string unicodeScalars value, convert to hexa and finish padding it with leading zeros:

extension String {
    var unicodes: [String] {
        return unicodeScalars.map{ String($0.value, radix: 16) }
    }
    func paddingToLeft(maxLength: Int) -> String {
        return repeatElement("0", count: max(0,maxLength-count)) + self
    }
}

let str =  "\u{00e2}"
let hexa = str.unicodes.first!
print(hexa.paddingToLeft(maxLength: 4))  // "00e2\n"
Leo Dabus
  • 229,809
  • 59
  • 489
  • 571