perl提供了两个函数进行数字和字母之间的转换。Ord函数将字母转换成数字,chr函数将数字转换成字母。特别当处理的HTML数据需要被转换并被储存的时候,使用这些函数使用特别容易。
Chr将数字转换成相应的字母。比如,chr(74)返回J。
Ord函数和chr函数刚好相反,它返回参数第一个字母的数字值,比如,ord(‘J’)返回74。
下面是chr和ord函数的实例。
s/([ &;:%])/sprintf "%%%02X", ord()/ge;
# replace space, &, ;, % and : with "%xx" where 'xx' is two
hex digits representing the character
s/(&#(d+);?)/chr()/eg; # replace all sequences of '&#xx;' with
the appropriate character representing the number 'xx'
Ord和chr函数使在协议中的数据比如HTML和XML中转换和存储变得非常容易。