17.7.5 Character and Numeric values: ord() and chr()

The ordchr extension adds two functions, named ord() and chr(), as follows:

@load "ordchr"

This is how you load the extension.

number = ord(string)

Return the numeric value of the first character in string.

char = chr(number)

Return a string whose first character is that represented by number.

These functions are inspired by the Pascal language functions of the same name. Here is an example:

@load "ordchr"
...
printf("The numeric value of 'A' is %d\n", ord("A"))
printf("The string value of 65 is %s\n", chr(65))

As of release 5.4 of gawk, the extension can handle multibyte / wide character sets. If given an invalid multibyte string, in a UTF-8 locale, ord() returns the Unicode “invalid character” value, 0xFFFD. Otherwise, it returns the numeric value of the first byte in the string. In a single-byte locale, ord() always returns the numeric value of the first byte in the string.