Log in

View Full Version : ord() | Return the ASCII value of a character


vampiredom
30th December 2011, 22:38
Here is a useful function I wrote for returning to ASCII value of a character, like the reverse of chr()

Note that AviSynth seems to ignore CHR(0) in strings ... A blank string will always return 0.

# requires Gscript.dll
global __ord_ASCII__ = ""

function ord(string c) {
c = LeftStr(c,1)

Gscript("""
if (__ord_ASCII__ == "") {
for (i=1, 255, 1) {
global __ord_ASCII__ = __ord_ASCII__ + chr(i)
}
}
""")

return (c == "") ? 0 : FindStr(__ord_ASCII__, c)
}

Gavino
30th December 2011, 23:04
global __ord_ASCII__ = ""
...
if (__ord_ASCII__ == "") {
for (i=1, 255, 1) {
__ord_ASCII__ = __ord_ASCII__ + chr(i)
}
}

This assignment is setting a local variable, so the loop will be redone on each call. Should be:
global __ord_ASCII__ = __ord_ASCII__ + chr(i)

vampiredom
30th December 2011, 23:19
Oops! Fixed. Thanks. [without that, it would kind of defeat the whole reason I wrote the function this way, wouldn't it? :)]