I cant understand when should I use pack and unpack functions in struct library in python? I also cant understand how to use them? After reading about it, what I understood is that they are used to convert data into binary. However when I run some examples like:
>>> struct.pack("i",34)
'"\x00\x00\x00'
I cant make any sense out of it. I want to understand its purpose, how these conversions take place, what does '\\x' and other symbols represent/mean and how does unpacking work.
I cant understand when should I use pack and unpack functions in struct library in python?
Then you probably don't have cause to use them.
Other people deal with network and file formats that have low-level binary packing, where struct
can be very useful.
However when I run some examples like:
>>> struct.pack("i",34) '"\\x00\\x00\\x00'
I cant make any sense out of it.
The \\x
notation is for representing individual bytes of your bytes
object using hexadecimal . \\x00
means that the byte's value is 0, \\x02
means that byte's value is 2, \\x10
means that that bytes value is 16, etc. "
is byte 34, so we see a "
instead of \\x22
in this view of the string, but '\\x22\\x00\\x00\\x00'
and '"\\x00\\x00\\x00'
are the same string
http://www.swarthmore.edu/NatSci/echeeve1/Ref/BinaryMath/NumSys.html might help you with some background if that is the level you need to understand numbers at.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.