How to return the ASCII code of a specific character in a string?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Leandro

Greetings. I’m trying to program a text routine for my game. For that I used a sprite atlas to load a transparent .png (.ttf and .fnt are not that interesting for me, considering the kind of text I want, see picture below).

Anyway I now need to program a function to read character by character from a string and return the ASCII code of that character so I can associate it to the correct frame from the sprite atlas. Any commands to help me on that? Thanks in advance!

BTW, there are two functions, substr() and to_ascii() that seem to do the trick, however, to_ascii() doesn’t return an integer, but a poolbytearray. How can I convert from poolbytearray to integer?

Leandro | 2018-12-14 20:24

:bust_in_silhouette: Reply From: flurick

This font style is generally called “college”, many complete fonts are available for example here.

But to answer the question. Converting text to ascii, character per character, and then converting that to a number sounds like the long way. Some ways to map names to values could be a directly with a boring dictionary, via str2var() or if you really want to do it via ascii; loop trough the array.

for i in ascii:

Thanks for the reply. I don’t need the whole ASCII table, just some characters (alphabet, algarisms and some punctuation signs). So far, I used substr() to get each character from the string, to_ascii() to get the ASCII code. The only step I need now is to convert this poolbytearray into a number to choose the right image to put on screen. :frowning:

Leandro | 2018-12-15 01:49