The Perl programming language's chr() and ord() functions are used to convert characters into their ASCII or Unicode values and vice versa. Chr() takes an ASCII or Unicode value and returns the equivalent character, and ord() performs the reverse operation by converting a character to its numeric value.
Perl编程语言的chr()和ord()函数用于将字符转换为ASCII或Unicode值,反之亦然。 Chr()采用ASCII或Unicode值并返回等效字符,而ord()通过将字符转换为其数值来执行相反的操作。
Perl Chr()函数 ( Perl Chr() Function )
The chr() function returns the character represented by the number specified. For example:
chr()函数返回由指定数字表示的字符。 例如:
#!/usr/bin/perl
#!/ usr / bin / perl
print chr (33)
打印字符(33)
print "/n";
打印“ / n”;
print chr (36)
打印CHR(36)
print "/n";
打印“ / n”;
print chr (46)
打印CHR(46)
print "/n";
打印“ / n”;
When this code is executed, it produces this result:
执行此代码后,将产生以下结果:
!
!
$
$
&
和
Note: The characters from 128 to 255 are by default not encoded as UTF-8 for backward compatibility reasons.
注意:出于向后兼容的原因,默认情况下从128到255的字符未编码为UTF-8。
Perl的Ord()函数 ( Perl's Ord() Function )
The ord() function does the opposite. It takes a character and converts it into its ASCII or Unicode numeric value.
ord()函数的作用与此相反。 它接受一个字符并将其转换为ASCII或Unicode数字值。
#!/usr/bin/perl
#!/ usr / bin / perl
print ord ('A');
打印订单('A');
print "/n";
打印“ / n”;
print ord ('a');
打印ord('a');
print "/n";
打印“ / n”;
print ord ('B');
打印ord('B');
print "/n";
打印“ / n”;
When executed, this returns:
执行后,将返回:
65
65
97
97
66
66
You can confirm the results are accurate by checking an ASCII Code Lookup Table online.
您可以通过在线检查ASCII码查找表来确认结果是否正确。
关于Perl ( About Perl )
Perl was created in the mid-'80s, so it was a mature programming language long before websites exploded in popularity. Perl was originally designed for text processing, and it is compatible with HTML and other markup languages, so it quickly became popular with website developers. Perl's strength lies in its ability to interact with its environment and its cross-platform compatibility. It can easily open and manipulate many files within the same program.
Perl创建于80年代中期,因此它是一种成熟的编程语言,早于网站开始流行。 Perl最初是为文本处理而设计的,它与HTML和其他标记语言兼容,因此很快在网站开发人员中流行。 Perl的优势在于与环境交互的能力以及跨平台的兼容性。 它可以轻松地打开和处理同一程序中的许多文件。
翻译自: https://www.thoughtco.com/perl-chr-ord-functions-quick-tutorial-2641190