Perl ord Function
The ord function is used to convert a character into its ASCII/Unicode corresponding value.
The ord function is used to convert a character to its ASCII or Unicode numeric value. The reverse of this function is the chr function which is used to convert an ASCII or Unicode value into its equivalent character.
The syntax forms of the Perl ord function are the follows:
- has as argument an expression
- returns the value of the first character of EXPR
If the ord function has no argument, the $_ is used instead.
See the following simple example of the ord function usage:
The following example shows you how to use the ord function in conjunction with the split and map functions.
- the split function is used with a null string as pattern, so the $str will be split into separate characters
- the map function will run the ord function for each element of the @array; at each iteration step the current element is assigned in turn to $_ and next the Perl ord function will act against the special variable $_; the map function returns the same array but having ASCII values as elements
- the elements of the @array are printed using the space delimiter; if you print an array enclosed in double quotes, its elements will be printed separated by space (the array will be interpolated)
The one-line map function:
A quicker alternative to do this is by using the unpack function, as in the following example:
Please note that there is an important difference between the ord and the unpack functions. Whereas ord works on characters, the unpack "C" function works on bytes. If you use the standard ASCII character set, you’ll get the same result.
But if you use Unicode, the unpack function will behave the same meanwhile the ord function will deal with the full Unicode character, which may be longer than 1 byte.
You can use the ord and pack functions to convert binary numbers in decimal. See an example in the following code snippet:
This method is not suitable for larger strings. An alternative for larger strings is to use pack and unpack or the Bit::Vector module.
Please have a look at the following short code snippet:
- the $str scalar variable will be converted to its hexadecimal corresponding value using the s/searchpattern/replacement/modifiers substitution regex operator and the bind operator =~
- (.) This is the search pattern: the dot means we match any single character and the using of the round parentheses allow us to store the matched character in the special variable named $1 (if you have more parentheses, the expression included in the second parenthesis will be assigned to $2, and so on)
- sprintf("%x",ord($1)) is the replacement argument of the substitution operator and it is pure Perl code; the Perl ord function returns the ASCII numeric value of the character stored in $1; next sprintf will convert this numeric value into its hexadecimal corresponding value (you can use %X if you want the hex values in uppercase)
- e is a modifier and it tells the regex engine to treat the replacement field as Perl code (see above)
- g is a modifier and tells the regex engine to repeatedly apply the substitution for all the characters of the string, starting with the first one