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

后端 未结 14 1187
陌清茗
陌清茗 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:49

    Swift 4.2

    The easiest way to get ASCII values from a Swift string is below

    let str = "Swift string"
    for ascii in str.utf8 {
        print(ascii)
    }
    

    Output:

    83
    119
    105
    102
    116
    32
    115
    116
    114
    105
    110
    103
    
    0 讨论(0)
  • 2020-11-29 01:49

    Swift 4+

    Char to ASCII

    let charVal = String(ch).unicodeScalars
    var asciiVal = charVal[charVal.startIndex].value
    

    ASCII to Char

    let char = Character(UnicodeScalar(asciiVal)!)
    
    0 讨论(0)
提交回复
热议问题