"Python" between ByteArray and string, used to process binary files and data streams

Source: Internet
Author: User

Recently in the use of Python serial tools, serial data stream basic reading and writing to rely on ByteArray, and we from the PYQT serial port to get the data are the string format, then we must consider how to convert the two data, in order to correctly send and receive data.

First consider the receiving serial data, then the format is ByteArray, the following needs to be processed into a string format to display:

#按string来显示, Byarray represents received Data Readstr = Byarray.decode (' utf-8 ') #这样就直接转换成str格式 # cast readstr = str (byarray) # The data obtained in this way will have the B ' character # to display the data read in hexadecimal characters, allowing us to directly see the lowest data format readstr = '. Join (Hex (x) for x in Byarray) # This sentence can be used to byarray the data into hex format, and with a space connected

Convert the string format to ByteArray:

= Wrstr.encode () #得到b' data

"Python" between ByteArray and string, used to process binary files and data streams

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.