26.27 取Unicode字符串的长整型值(little-endian序)
Q:
下面这段Python3代码显示Unicode字符串"www.baidu.com"前16字节的长整数形式,2 个长整数,little-endian序:
x = 'www.baidu.com' x = "".join([c+'\0' for c in x]).encode( 'latin-1' ).hex() y = x[:16] ''.join(map(str.add, y[-2::-2], y[-1::-2])) y = x[16:32] ''.join(map(str.add, y[-2::-2], y[-1::-2]))
'002e007700770077' '0064006900610062'
我只会这么矬的办法,不知更好的办法是啥?
A: bluerust 2020-03-07
Python3
import struct
x = "www.baidu.com" x = bytes( x, encoding='utf-16' )[2:] [hex(struct.unpack( "<Q", x[i:i+8] )[0]) for i in range(0 ,len(x)//8*8, 8)]
['0x2e007700770077', '0x64006900610062', '0x6f0063002e0075']
import hexdump
x = "www.baidu.com" x = bytes( x, encoding='utf-16' )[2:] ['0x' + ''.join(map(str.add, y[-2::-2], y[-1::-2])) for y in hexdump.dump( x, size=16, sep=',' ).split( ',' )]
import hexdump
x = "www.baidu.com" x = bytes( x, encoding='utf-16' )[2:] ['0x' + ''.join(map(str.add, y[-2::-2], y[-1::-2])) for y in hexdump.dump( x, size=16 ).split()]
['0x002E007700770077', '0x0064006900610062', '0x006F0063002E0075', '0x006D']