如何在 Nim 中将字符转换为 ASCII 代码,反之亦然

Seb*_*Kas 3 nim-lang

如何将字符转换为十进制 ASCII 码?

因此例如"a"应该转换为97.

Seb*_*Kas 5

我自己想出来了。

您可以将 a 转换charintwith:int('a')ord('a')

"a" in Nim is a string, not a char. So at first you need to get the character you want the ASCII code from. In this case the first character inside the string.

So int(char("a"[0])) would give the ASCII code for the first character inside a string.

The same procedure works also in the other direction. To convert an ASCII code into a character, you do char(97) and get 'a'.