Print unicode character from variable (swift)

前端 未结 6 425
耶瑟儿~
耶瑟儿~ 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:26

    As of Swift 2.0, every Int type has an initializer able to take String as an input. You can then easily generate an UnicodeScalar corresponding and print it afterwards. Without having to change your representation of chars as string ;).

    UPDATED: Swift 3.0 changed UnicodeScalar initializer

    print("\u{1f44d}") //                                                                     
    0 讨论(0)
  • 2020-11-30 15:28

    Here are a couple ways to do it:

    let string = "1f44d"
    

    Solution 1:

    "&#x\(string);".applyingTransform(.toXMLHex, reverse: true)
    

    Solution 2:

    "U+\(string)".applyingTransform(StringTransform("Hex/Unicode"), reverse: true)
    
    0 讨论(0)
  • 2020-11-30 15:29

    You can use

    let char = "-12"
    print(char.unicodeScalars.map {$0.value }))
    

    You'll get the values as:

    [45, 49, 50]
    
    0 讨论(0)
  • 2020-11-30 15:35

    This can be done in two steps:

    1. convert charAsString to Int code
    2. convert code to unicode character

    Second step can be done e.g. like this

    var code = 0x1f44d
    var scalar = UnicodeScalar(code)
    var string = "\(scalar)"
    

    As for first the step, see here how to convert String in hex representation to Int

    0 讨论(0)
  • 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) //                                                                     
    0 讨论(0)
  • 2020-11-30 15:35

    You cannot use string interpolation in Swift as you try to use it. Therefore, the following code won't compile:

    let charAsString = "1f44d"
    print("\u{\(charAsString)}")
    

    You will have to convert your string variable into an integer (using init(_:radix:) initializer) then create a Unicode scalar from this integer. The Swift 5 Playground sample code below shows how to proceed:

    let validCodeString = "1f44d"
    let validUnicodeScalarValue = Int(validCodeString, radix: 16)!
    let validUnicodeScalar = Unicode.Scalar(validUnicodeScalarValue)!
    print(validUnicodeScalar) //                                                                     
    0 讨论(0)
提交回复
热议问题