Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a character to an ASCII code & vice versa in Nim

Tags:

nim-lang

How to convert a character to a dezimal ASCII code?

So for example "a" should be converted to 97.

like image 864
SebKas Avatar asked Sep 05 '25 22:09

SebKas


1 Answers

I figured it out by myself.

You can cast a char into an int with: int('a') or 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'.

like image 76
SebKas Avatar answered Sep 11 '25 03:09

SebKas