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:

ord EXPR
ord
This function:
  • 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:

#!/usr/local/bin/perl
 
use strict;
use warnings;
 
print ord('A'), "\n";
# it displays: 65

The following example shows you how to use the ord function in conjunction with the split and map functions.

#!/usr/local/bin/perl
 
use strict;
use warnings;
 
# initialize a string
my $str = "Hello World!";
 
# split the string into an array of characters
my @array = split //,$str;
 
# converts the elements of the array into their
# equivalent ASCII codes
@array = map(ord, @array);
 
# print the array with spaces between elements
print "@array\n";
# it outputs: 72 101 108 108 111 32 87 111 114 108 100 33
I want to make a few considerations about this code.
  • 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:

@array = map(ord, @array);
can be rewritten with any of the following code lines:
 
@array = map (ord $_, @array);
@array = map { ord } @array;
@array = map { ord $_ } @array;
By the way, calling the map function with the expression syntax form is faster than calling it with the block syntax form.

A quicker alternative to do this is by using the unpack function, as in the following example:

#!/usr/local/bin/perl
 
use strict;
use warnings;
 
# initialize a string
my $str = "Hello World!";
my @array = unpack("C*", $str);
print "@array\n";
# it displays: 72 101 108 108 111 32 87 111 114 108 100 33

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:

#!/usr/local/bin/perl
 
use strict;
use warnings;
 
my $dec = ord pack('B8','01100110');
print "$dec\n";
# it prints: 102
The pack function with the 'B' template converts the string consisting of 0 and 1 characters to a byte, and the bits are specified in decreasing order from LSB to MSB (see this template of the pack function for more details). The byte returned by the pack function is converted into its ASCII value by the Perl ord function.

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:

#!/usr/local/bin/perl
 
use strict;
use warnings;
 
# initialize a string
my $str = "Hello World!";
 
$str =~ s/(.)/sprintf("%x", ord($1))/eg;
print "$str\n";
# it displays: 48656c6c6f20576f726c6421
I want to make a few remarks about how this snippet works:
  • 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