Perl chr Function
last modified October 8, 2026
The Perl chr function converts ASCII or Unicode code points to
their corresponding characters. It's the inverse of Perl's ord
function.
chr takes a number and returns the character represented by that
number in the current character set. It works with both ASCII and Unicode
values.
Basic chr Usage
The simplest way to use chr is to convert ASCII values to
characters.
#!/usr/bin/perl use strict; use warnings; use v5.34.0; my $char = chr(65); print "Character for ASCII 65: '$char'\n"; $char = chr(97); print "Character for ASCII 97: '$char'\n";
We demonstrate chr converting ASCII values to their corresponding
characters. ASCII 65 is 'A' and 97 is 'a'.
$ ./basic.pl Character for ASCII 65: 'A' Character for ASCII 97: 'a'
Generating Alphabet
chr can be used to generate sequences of characters.
#!/usr/bin/perl
use strict;
use warnings;
use v5.34.0;
print "Uppercase alphabet: ";
for my $i (65..90) {
print chr($i);
}
print "\n";
print "Lowercase alphabet: ";
for my $i (97..122) {
print chr($i);
}
print "\n";
This script generates the English alphabet using ASCII values. The uppercase letters range from 65-90, lowercase from 97-122.
$ ./alphabet.pl Uppercase alphabet: ABCDEFGHIJKLMNOPQRSTUVWXYZ Lowercase alphabet: abcdefghijklmnopqrstuvwxyz
Working with Unicode
chr accepts Unicode code points greater than 255; the output encoding determines how they are displayed.
#!/usr/bin/perl use strict; use warnings; use v5.34.0; binmode(STDOUT, ':encoding(UTF-8)'); my $euro = chr(0x20AC); my $heart = chr(0x2665); my $smiley = chr(0x1F600); print "Euro symbol: $euro\n"; print "Heart symbol: $heart\n"; print "Smiley face: $smiley\n";
We generate several Unicode characters using their hexadecimal code points.
Since the characters are beyond the ASCII range, we set the standard output to
UTF-8 with binmode; otherwise Perl prints a "Wide character"
warning. Terminal fonts must support the characters for them to display
correctly.
$ ./unicode.pl Euro symbol: € Heart symbol: ♥ Smiley face: 😀
Creating Special Characters
chr is useful for creating control characters and special symbols.
#!/usr/bin/perl use strict; use warnings; use v5.34.0; my $newline = chr(10); my $tab = chr(9); my $bell = chr(7); print "This ends with newline$newline"; print "This\tuses\ttabs$newline"; print "Bell character code: ", ord($bell), "\n";
This example creates common control characters. ASCII 10 is newline, 9 is tab,
and 7 is the bell character. The bell character is not visible on screen, so
the script prints its code with ord instead.
$ ./special.pl This ends with newline This uses tabs Bell character code: 7
Binary Data Generation
chr can help generate binary data by converting numeric values.
#!/usr/bin/perl
use strict;
use warnings;
use v5.34.0;
my $binary = '';
for my $i (0..255) {
$binary .= chr($i);
}
open(my $fh, '>', 'data.bin') or die $!;
print $fh $binary;
close($fh);
print "Generated binary file with all 256 byte values\n";
We create a binary file named data.bin containing all possible
byte values (0-255). Each number is converted to its corresponding byte using
chr. The file is 256 bytes long.
$ ./binary.pl Generated binary file with all 256 byte values
Password Generation
chr can be part of random password generation algorithms.
#!/usr/bin/perl
use strict;
use warnings;
use v5.34.0;
sub generate_password {
my $length = shift;
my $password = '';
for (1..$length) {
# Generate random printable ASCII (33-126)
my $char = chr(33 + int(rand(94)));
$password .= $char;
}
return $password;
}
print "Your password: ", generate_password(12), "\n";
This script generates random passwords using printable ASCII characters.
chr converts random numbers to their character equivalents. The
sample output below is only one possible result; the password differs on every
run. This example is for illustration only; use a cryptographically secure
source of randomness for real passwords.
$ ./password.pl Your password: kL9#mX@2qP$5
Hex String Conversion
chr can convert hexadecimal string representations to characters.
#!/usr/bin/perl
use strict;
use warnings;
use v5.34.0;
my $hex_str = "48656c6c6f20576f726c64"; # "Hello World" in hex
my $string = '';
while ($hex_str =~ /(..)/g) {
$string .= chr(hex($1));
}
print "Converted string: $string\n";
We convert a hexadecimal string to its ASCII representation. Each pair of hex
digits is converted to a character using chr.
$ ./hex.pl Converted string: Hello World
Best Practices
- Set the output encoding: Use UTF-8 output for characters beyond ASCII.
- Validate input: Ensure numbers are within valid ranges.
- Combine with ord: Use with ord for character analysis.
- Document encoding: Clearly specify character encoding used.
Source
This tutorial covered Perl's chr function with practical
examples demonstrating its usage in various scenarios.
Author
List all Perl tutorials.