Print unicode character from variable (swift)

前端 未结 6 449
耶瑟儿~
耶瑟儿~ 2020-11-30 14:53

I have a problem I couldn\'t find a solution to. I have a string variable holding the unicode \"1f44d\" and I want to convert it to a unicode character

6条回答
  •  时光说笑
    2020-11-30 15:35

    One possible solution (explanations "inline"):

    let charAsString = "1f44d"
    
    // Convert hex string to numeric value first:
    var charCode : UInt32 = 0
    let scanner = NSScanner(string: charAsString)
    if scanner.scanHexInt(&charCode) {
    
        // Create string from Unicode code point:
        let str = String(UnicodeScalar(charCode))
        println(str) // 

提交回复
热议问题