Python byte conversion: struct

Source: Internet
Author: User

Official website Symbol Description: https://docs.python.org/3/library/struct.html#format-characters



Splits a 32-bit unsigned int into 4 bytes: pack

Import Structprint (Struct.pack (' >i ', 10240099)) # ' > ' represents the conversion to Big-endian, that is, the network byte order, ' I ' means ' 10240099 ' is a 4 byte unsigned integer # number of shapes D = struct.pack (' <i ', 10240099) # ' < ' for conversion to Little-endianprint (d)

Operation Result:

B ' \x00\[email protected] ' b ' [email protected]\x9c\x00 '


Combine the corresponding number of digits into one count: unpack

A = Struct.pack (' >i ', 10240099) b = struct.unpack (' >i ', a) #将a bytes are converted to a 4-byte unsigned integer by Big-endian print (b) c = struct.un Pack (' >ih ', b ' \xf0\xf0\xf0\xf0\x80\x80 ') print (c)

Operation Result:

(10240099,) (4042322160, 32896) # (I, H)



Python byte conversion: struct

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.