-
-
python HEX打印字符串
-
发表于:
2021-12-5 14:46
8332
-
作为一个多年的c++程序员,看python有点别扭
最直接的就是不方便开内存,比如我们最喜欢的就是下面的形式
1 2 3 4 5 6 | 31 32 33 34 35 36 37 38 39 30 61 62 63 64 65 66 1234567890abcdef
67 68 69 6a 6b 6c 6d 6e 31 32 33 34 35 36 37 38 ghijklmn12345678
39 30 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 90abcdefghijklmn
31 32 33 34 35 36 37 38 39 30 61 62 63 64 65 66 1234567890abcdef
67 68 69 6a 6b 6c 6d 6e e4 b8 ad e5 9b bd ghijklmn......
Press any key to continue . . .
|
在网上找半天没有发现,自己写了一个,代码写得很垃圾哈,我也不懂python,凑合用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | def print_hex(bytes):
cycle = len (bytes) / / 16
for i in range (cycle):
lhex = ""
lascii = ""
for y in range ( 16 ):
lhex + = '{:0>2x}' . format ( int (bytes[i * 16 + y]))
lhex + = " "
if chr (bytes[i * 16 + y]).isascii():
lascii + = chr (bytes[i * 16 + y])
else :
lascii + = "."
print (lhex + lascii)
n = len (bytes) % 16
lhex = ""
lascii = ""
for i in range (n):
lhex + = '{:0>2x}' . format ( int (bytes[cycle * 16 + i]))
lhex + = " "
if chr (bytes[cycle * 16 + i]).isascii():
lascii + = chr (bytes[cycle * 16 + i])
else :
lascii + = "."
print ( "%-47s %s" % (lhex, lascii))
ta = "1234567890abcdefghijklmn1234567890abcdefghijklmn1234567890abcdefghijklmn"
ta + = "中国"
test = b "\xab\xcd\xef\x11\x001111111111111111111111111111111111111111111111111111111"
print_hex(test)
print_hex(ta.encode())
|
我也提交了一个github,有问题提交一下,方便大家好
wannianhong/python_print_hex (github.com)
[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课