我希望记录剪切并粘贴到bash作为参数的字符串的十六进制Unicode码点。ord无法实现这一点;ord似乎只适用于ASCII范围内。关于ord的大部分信息都至少有六年或更长时间,且不再相关,因为我正在使用已内置Unicode支持的v5.24。在Python中很容易实现:
for i in unicode(sys.argv[1], 'utf-8'):
print i.encode("utf_16_be").encode("hex")
这段代码是在bash中执行的。我认为问题出在ord函数本身,它似乎没有更新以支持Unicode。
# ord.pl does not provide the unicode code point for a pasted variable.
use strict;
use warnings;
#use charnames (); #nope.
#use feature 'unicode_strings'; #nope. Already automatically using as of v5.12.
#use utf8; #nope.
#binmode(STDOUT, ":encoding(UTF-8)"); #nope.
my $arg = "";
foreach $arg (@ARGV) {
print $arg . " is " . ord($arg) . " in code.\n"; # seems to me ord is ascii only.
#utf8::encode($arg); #nope.
#print unpack("H*", $arg) . "\n"; #nope.
#printf "%vX\n", $arg; #nope.
}
转化为:
david@A8DT01:~/bin$ ord.pl A B C D a b c d \ \\ … — €
A is 65 in code.
41
B is 66 in code.
42
C is 67 in code.
43
D is 68 in code.
44
a is 97 in code.
61
b is 98 in code.
62
c is 99 in code.
63
d is 100 in code.
64
is 32 in code.
20
\ is 92 in code.
5c
… is 226 in code.
c3a2c280c2a6
— is 226 in code.
c3a2c280c294
is 239 in code.
c3afc280c2a8
€ is 226 in code.
c3a2c282c2ac
david@A8DT01:~/bin$
我希望能够在Python中得到与以下输出相同的结果:
david@A8DT01:~/bin$ python code-points.py "ABCDabcd \ … — €"
0041
0042
0043
0044
0061
0062
0063
0064
0020
005c
0020
2026
0020
2014
0020
f028
0020
20ac
david@A8DT01:~/bin$
-CSDA
运行您的脚本:即perl -CSDA ord.pl A B C D a b c d \ \\ ... — €
。这对我有效。 - Håkon Hægland