Character codes: Difference between revisions

Added Scala
m (→‎{{header|J}}: remove lang tags - they interfere with UTF8 symbols)
(Added Scala)
Line 329:
> 97.chr
=> "a"</lang>
 
=={{header|Scala}}==
Scala supports unicode characters, but each character is UTF-16, so
there is not a 1-to-1 relationship for supplementary character sets.
 
Without worrying about supplemental character sets:
 
<lang scala>scala> 'a' toInt
res9: Int = 97
 
scala> 97 toChar
res10: Char = a</lang>
 
Worrying about supplemental character sets, we need to test the "next"
character as well:
 
<lang scala>def charToInt(c: Char, next: Char): Option[Int] = (c, next) match {
case _ if (c.isHighSurrogate && next.isLowSurrogate) => Some(java.lang.Character.toCodePoint(c, next))
case _ if (c.isLowSurrogate) => None
case _ => Some(c.toInt)
}
 
def intToChars(n: Int): Array[Char] = java.lang.Character.toChars(n)</lang>
 
=={{header|Scheme}}==
Anonymous user