python–ntohll和htonll的实现(转载)

可以利用python中的struct包中的如下东东进行转换:
1、字节序:
Character Byte order Size and alignment
@ native native
= native standard
< little-endian standard
> big-endian standard
! network (= big-endian) standard

2、字节数
Format C Type Python Notes
x pad byte no value  
c char bytes of length 1  
b signed char integer (1)
B unsigned char integer  
? _Bool bool (2)
h short integer  
H unsigned short integer  
i int integer  
I unsigned int integer  
l long integer  
L unsigned long integer  
q long long integer (3)
Q unsigned long long integer (3)
f float float  
d double float  
s char[] bytes (1)
p char[] bytes (1)
P void * integer  

3、自定义函数

使用struct,相当轻松

import struct
htonll =
lambda a:struct.unpack('!Q', struct.pack('Q', a) )[0]
ntohll = lambda a:struct.unpack('Q', struct.pack('!Q', a))[0]








原文地址:https://www.cnblogs.com/huangjacky/p/2661622.html