首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > perl python >

python struct (对python obj进展编码解码)

2013-12-11 
python struct (对python obj进行编码解码)# -*- coding: utf-8 -*-import structimport sysimport os1

python struct (对python obj进行编码解码)
# -*- coding: utf-8 -*-import structimport sysimport os'''1. Byte order, Size, Alignment-------------------------------------------------------------Character Byte order Size Alignment-------------------------------------------------------------@ native native native ==> default= native standard none< little-endian standard none> big-endian standard none! network (= big-endian) standard none-------------------------------------------------------------1). byte order: Native byte order is big-endian or little-endian, depending on the host system. ①Intel X86 / AMD64(x86-64) ==> little-endian ②Motorola 68000 / PowerPC G5 ==> big-endian ③ARM / Intel Itanium feature switchable endianness (bi-endian)2). size: ①Native size are determined using the C compiler's sizeof expression. ②Standard size depends only on the 'format character'3). alignment:2. format characterFormat C Type Python type Standard sizex pad byte no value c char string of length 1 1 b signed char integer 1 B unsigned char integer 1? _Bool bool 1 h short integer 2 H unsigned short integer 2 i int integer 4 I unsigned int integer 4 l long integer 4L unsigned long integer 4 q long long integer 8 Q unsigned long long integer 8 f float float 4 d double float 8 s char[] string p char[] string P void * integer '''#检查大端小端print sys.byteorders='abcde'a=20b=400'''3. python obj-->pack-->str'''packed_str = struct.pack('< 5s2i',s,a,b)#< : little endian, standard side, none alignment# whitespaces are ignored.#5s : 5 is the "size" of the string#2i : 2 is the "repeat count"#print len(s)print struct.calcsize('< 5s2i')print repr(s)print type(s)'''4. str-->pack-->python obj'''(s, a, b) = struct.unpack('<5s2i', packed_str)print sprint aprint b

?

?

?

热点排行