Char or String -> Unicode value in Scala?

If you convert each RichChar of String .toLong it's also works. For example, this:

str.map (_.toLong).product - work's fine and without foldLeft or cycles

This is cyclic variant:

def product(str: String): Long = {
    var prod: Long = 1
    for (ch <- str) prod *= ch
    prod
}

When you do x.foldLeft(1)(_ * _.toInt), the result type will be inference to an Int, but 9415087488 is too large for an Int to store it.

So you need to tell Scala using Long to store it.

scala> val x = "Hello"
x: java.lang.String = Hello

scala> x.foldLeft(1L)(_ * _.toInt)
res1: Long = 9415087488

scala> var x: Long = 1
x: Long = 1

scala> for (c <- "Hello") x *= c.toInt

scala> x
res7: Long = 9415087488

Tags:

Unicode

Scala