Amir Afghani Amir Afghani - 1 year ago 127
Scala Question

Scala conversion of Strings

When I run the following program :

object Problem {
def main(args: Array[String]) = {
val v = 53.toString
val w = v(0).toInt

it prints out
, instead of what I would have expected,
. Can someone help me understand why?

UPDATE: The same thing happens if I use
instead of the array syntax

Answer Source

53 is the ASCII value for the character '5'. Try 63.toString and you'll see 54 after v(0).toInt.

Use .asDigit to convert a Char to it's Int value. In other words, '5'.toInt == 53 but '5'.asDigit == 5.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download