Karl
Karl

Reputation: 2187

how can I find out the ASCII code of a character in gnuplot

Does anybody know how to find out the ascii code of a character in gnuplot?

There is no official function to do that, so it must probably be some kind of trick.

(OK, i found a way, answer directly below)

Upvotes: 1

Views: 386

Answers (1)

Karl
Karl

Reputation: 2187

In lieu of an ord(ch) function, one can build a string with all characters, and find the position of the one in question using the strstrt()function.

# make a string that contains all ASCII chars from 1 to 255
ALLCHARS = ''; do for [i=1:255] {ALLCHARS = ALLCHARS.sprintf('%c',i)}
# return position of character in ALLCHARS if ch contains 1 char, -1 otherwise
ord(ch) = (strlen(ch) == 1) ? strstrt(ALLCHARS,ch): -1

# test with ASCII char 12
pr n=12, testch = sprintf('%c',n), ord(testch)

The NUL (ASCII-code zero) character is missing, because gnuplot anyway has no single character variable type that could hold it. gnuplot strings are NUL terminated.

Upvotes: 3

Related Questions