In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
It is believed that many inexperienced people have no idea about how to use the struct template in Python. Therefore, this article summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
The main functions of struct template are:
Pack (v1, v2,...)
Unpack (string)
Pack_into (buffer, offset, v1, v2,...)
Unpack_from (buffer, offset=0)
The following are introduced one by one
Pack () and unpack () pack ()
Let's first take a look at the official instructions:
Pack (fmt, v1, v2,...):
Return a string containing the values v1, v2,... Packed according to the given format. The arguments must match the values required by the format exactly.
Is to convert values:v1, v2 into string according to the corresponding fmt (format) mode.
Take a look at a chestnut:
> import struct > v1 = 1 > > v2 = 'abc' > bytes = struct.pack (' i3sgiving, v1, v2) > bytes'\ X01\ X00\ x00\ x00abc'
"fmt" here means "i3s". What does it mean? Where I is integer, that is, an integer, and the following s corresponds to string. In the chestnut above, abc is a string of length 3, so you have 3s.
Here is a complete list of fmt:
Fmt.png
Unpack ()
Again, take a look at the official documentation first
Unpack (fmt, string)
Unpack the string (presumably packed by pack (fmt,...)) According to the given format. The result is a tuple even if it contains exactly one item. The string must contain exactly the amount of data required by the format (len (string) must equal calcsize (fmt)).
To put it simply, the string is parsed according to the corresponding fmt form. Notice that the result returns a tuple
Take a chestnut.
> bytes ='\ X01\ X00\ X00\ x00abc' > > v1, v2 = struct.unpack ('i3sfolk, bytes) > v11 > v2roomabc'
This will restore the above v1Magee v2 back.
Note that when there is only one return value:
> a = 2 > a_pack = struct.pack > a_unpack = struct.unpack (`ipright packs) # the a_unpack obtained here is tuple > a_unpack (2,) > > a_unpack, = struct.unpack (`iLianzhongpacks) # the a_unpack obtained here is int > a_unpack2Byte Order, Size, and Alignment
This is interspersed with the order, size, and alignment of bytes.
Byte order
There is a table below
Order.png
If you add''to the fmt string, it will be arranged in big-endian, that is, the big end. The default is the'@ 'mode
Take a chestnut.
> a = 2 > a_pack = struct.pack ('iLifepoint a) # this is the default, different machines may be different By default, the bytes here are sorted in little-endian order > a_pack2 = struct.pack ('> itemology a) #'> 'that is, big-endian > aintpacks pack 2'\ X00\ x00\ x00\ x02'> a_pack3 = struct.pack ('iLifePal a) # big-endian > aintpack 2'\ X00\ x00\ x00\ x02' > > a_unpack, = struct.unpack (' ibirthPack _ pack2) # big-endian > a_unpack22
As shown above, if the byte order of pack and unpack operations are not consistent, messing up little-endian and big-endian will lead to data confusion.
Size and alignment
In fact, struct is similar to the struct structure in C language to store data. So there is a problem with data alignment. If you are on a 32-bit (that is, 4GB) machine with memory, it is generally aligned at 4 bytes. CPU reads 4 bytes at a time and puts them into the corresponding cache (cache).
Look at a chestnut.
Struct A {char C1; int a; char c2;}
How much memory will structure A take up? Intuitively, it may be 1 "4" 1 = 6 bytes, but generally speaking, it is actually 12 bytes! After the first char variable C1 takes up a byte, because it is 4 bytes aligned, the int variable a will not be inserted after C1, but will implicitly add three bytes after C1, and then put an on the line below, and finally put the char variable c2 under a.
Take a look at the following.
Struct A {char C1; char c2; int a;}
In this case, how much memory will structure A take up? The answer is 8 bytes. The principle is the same as above, first put the char variable C1 on, there are 3 bytes in the same line with C1, as soon as you see the next char variable c2 only 1 byte, then connect c2 after C1, at this time there are 2 bytes left, but it is not enough int, so you can only fill 2 bytes and then start another line.
Why do you think so? Isn't this a waste of memory? In a sense, it is a waste of memory, but it improves the efficiency of CPU!
Think about this scenario pattern: suppose a line in memory has already put a byte of char variable c, and the next is the int variable a, which takes up a total of 4 bytes of memory, first putting 3 bytes after variable c, and then putting the last 1 byte on the following line.
What if CPU wants to read the a variable? It should be read twice! Read 3 bytes at a time, 1 byte at a time. So the speed is really slow, it's twice as slow! If the variable an is on a different line, it is enough to read it once and take away 4 bytes directly.
Calcsize ()
With a simple understanding of the above, we can understand what this function is for.
The document gentleman said
Struct.calcsize (fmt)
Return the size of the struct (and hence of the string) corresponding to the given format.
To put it simply, it is based on fmt to calculate how many bytes of memory struct takes up.
Take a chestnut.
> struct.calcsize ('ci') 8 > struct.calcsize (' ic') 5
Looking up the format table above, c corresponds to char with a size of 1 byte, and I corresponds to int with a size of 4 bytes. Therefore, there is the above situation, as for the reason, it is no longer cumbersome. It's just that the final ic outputs 5, and I guess the last line in the memory line occupied by struct is populated without padding.
The chestnuts above are all filled with padding. What if they are not filled?
> > struct.calcsize ('
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.