1

thanks for looking and possibly responding

val c1 = 'a' + 1
val c2 = 'a' + 25
val c3 = 'E' - 2

// 'a' + 1
val c1 = 'b'

// 'a' + 25
val c2 = ??

// 'E' - 2
val c3 = 'C'

complete noob, why does val c2 = z

I cant understand how 86 translates to 'z'. The unicode table does not give a character that represents 86.

Waqar
  • 13
  • 4
  • Why do you think that `c2` is `88`? – Marvin Jan 13 '23 at 11:19
  • thanks for responding, im learning as i go, if a small case a is U+0061 then 61 + 25 = 86, WHOOPS, not 88, i guess i'll be editing my question first – Waqar Jan 13 '23 at 11:28
  • 2
    You're mixing hexadecimal and decimal. `'a'` is 61 hex, i.e. 97 decimal. So it's 97 +25 == 122 (7A hex). – Michael Jan 13 '23 at 11:29
  • The letter is 'z' is 25 points ahead of the letter 'a'. Does this give you any hint? – Roslan Amir Jan 13 '23 at 11:44
  • okay so firstly, thanks for pointing me in the right direction guys. Sounds like I have a to add a few more topics to my reading list. All of your answers are very helpful but assume that i have the prerequisite knowledge, which i currently don't. Thanks again. – Waqar Jan 13 '23 at 12:30

1 Answers1

2

A Kotlin Char is, basically, just a regular number that represents a Unicode character (What are Unicode, UTF-8, and UTF-16?). Each number is assigned to a character, which we can look up in a unicode table. In this we can see that the letter a has a decimal representation of 97.

screenshot of unicode table, highlighting

You could also get the decimal value using Char.code

fun main() {
  println('a'.code)
}
97

Run in Kotlin Playground

Therefore, in decimal, 97 + 25 = 122.

Looking up 122 in the Unicode table reveals that this is the decimal representation of z. You can again use Char.code to get the decimal representation.

fun main() {
  println(('a' + 25).code)
  println('a' + 25)
}
122
z

Run in Kotlin Playground

aSemy
  • 5,485
  • 2
  • 25
  • 51