在 Perl 中将 UTF8 字符串转换为数值
例如,
my $str = '中國c'; # Chinese language of china
我想打印出数值
20013,22283,99
For example,
my $str = '中國c'; # Chinese language of china
I want to print out the numeric values
20013,22283,99
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
unpack
比split< 更高效/code> 和
ord
,因为它不必生成一堆临时的 1 字符字符串:快速基准测试显示它比
split+ord
快大约 3 倍:结果:
字符串较短时,差异不太明显,但
unpack
的速度仍然快两倍以上。 (split-for2
比其他拆分要快一些,因为它不构建代码点列表。)unpack
will be more efficient thansplit
andord
, because it doesn't have to make a bunch of temporary 1-character strings:A quick benchmark shows it's about 3 times faster than
split+ord
:Results:
The difference is less pronounced with a shorter string, but
unpack
is still more than twice as fast. (split-for2
is a bit faster than the other splits because it doesn't build a list of codepoints.)请参阅 perldoc -f ord:
或压缩为一行:
my @ chars = map { ord } split //, $str;
Data::Dumper ed,这会产生:
See perldoc -f ord:
Or compressed into a single line:
my @chars = map { ord } split //, $str;
Data::Dumpered, this produces:
要让源代码中的 utf8 被识别,您必须事先使用 utf8; :
或者更简洁地说,
To have utf8 in your source code recognized as such, you must
use utf8;
beforehand:or more tersely,
http://www.perl.com/pub/2012/04 /perlunicook-standard-preamble.html
http://www.perl.com/pub/2012/04/perlunicook-standard-preamble.html