What's the simplest way to convert from a single character String to an ASCII value in Swift?

后端 未结 14 1323
陌清茗
陌清茗 2020-11-29 01:00

I just want to get the ASCII value of a single char string in Swift. This is how I\'m currently doing it:

var singleChar = \"a\"
println(singleChar.unicodeSc         


        
14条回答
  •  醉酒成梦
    2020-11-29 01:27

    Swift 4.1

    https://oleb.net/blog/2017/11/swift-4-strings/

    let flags = "99_problems"
    flags.unicodeScalars.map {
        "\(String($0.value, radix: 16, uppercase: true))"
    }
    

    Result:

    ["39", "39", "5F", "70", "72", "6F", "62", "6C", "65", "6D", "73"]

提交回复
热议问题