Search code examples
swiftcharacterasciicoercion

Swift How to get an ASCII value from a character


I simply want to get a single ASCII value from a single character in swift for a cipher that I'm doing. With the input of "A" I want to receive 65 for example. However, with the code

var str:String = "A"
var temp = str.utf8
print(temp)

"A" is printed to the console rather than what I would expect to be 65. Does anyone know a simple solution for this? Everywhere I search seems to think I want to get the integer 5 from the character "5"


Solution

  • You can't get it from the String but from a Character in a string, for instance

    str.first?.asciiValue