Print unicode character from variable (swift)

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) // 👍
} else {
    println("invalid input")
}

Slightly simpler with Swift 2:

let charAsString = "1f44d"

// Convert hex string to numeric value first:
if let charCode = UInt32(charAsString, radix: 16) {
    // Create string from Unicode code point:
    let str = String(UnicodeScalar(charCode))
    print(str) // 👍
} else {
    print("invalid input")
}

Note also that not all code points are valid Unicode scalars,
compare Validate Unicode code point in Swift.


Update for Swift 3:

public init?(_ v: UInt32)

is now a failable initializer of UnicodeScalar and checks if the
given numeric input is a valid Unicode scalar value:

let charAsString = "1f44d"

// Convert hex string to numeric value first:
if let charCode = UInt32(charAsString, radix: 16),
    let unicode = UnicodeScalar(charCode) {
    // Create string from Unicode code point:
    let str = String(unicode)
    print(str) // 👍
} else {
    print("invalid input")
}

Leave a Comment