Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the binascii module in Python

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "how to use the binascius module in Python". In daily operation, I believe many people have doubts about how to use the binascius module in Python. Xiaobian consulted various materials and sorted out simple and easy operation methods. I hope to help you answer the doubts about "how to use the binascius module in Python"! Next, please follow the small series to learn together!

binascii module usage

binascii module for converting between binary and ASCII

>> import binascii#convert binary to ascii and represent it in hexadecimal>> str1 = b"hello world" >> binascii.b2a_hex(b"hello world")#output b'68656c6c6f20776f726c64'#do the opposite>> binascii.a2b_hex(b'68656c6f20776f726c64')#output b'hello world'>> binascii.hexlify(b"hello world") #Comment: Same as b2a_hex(), returns the hexadecimal representation of binary data. Each byte of data is converted to its corresponding 2-bit hexadecimal representation. So the byte object returned is twice the length of data.# output b'68656c6c6f20776f726c64'>> binascius.unhexlify(b'68656c6f20776f726c64') #Comment: Same as a2b_hex(), returns binary data represented by the hexadecimal string hexstr. hexstr must contain an even number of hexadecimal digits (upper or lower case), otherwise an Error exception is thrown.# Output b'hello world'binascii module and binary conversion notes

Cut the crap and go straight to the code:

# !/ usr/bin/env python# -*- coding: utf-8-*-# author: big watermelon #import binascii module import binascii a = b'BE27 E8 FFFF 010203'#convert b'BE27 E8 FFFF010203' to binary data first and then print out b = binascii.b2a_hex(a)#in hexadecimal notation: b'424532374538464646303130323033', for example, B corresponds to ascii code 42, E corresponds to ascii code 45print(b) #opposite to b2a_hex, print out: b'BE27E8FFFF010203'print (binascii.a2b_hex(b)) #This function is the same as b2a_hex()#Print: b'42453237453846464646303130323033', for example, B corresponds to ascii code 42, E corresponds to ascii code 45c = binascii.hexlify(a)print(c) #This function is the same as a2b_hex(), printing out: b'BE27E8FFFF010203'print(binascii.unhexlify(c))Python built-in function

hex(): decimal to hexadecimal

#Convert decimal to hexadecimal>> hex(88)'0x58'#Convert floating point to hexadecimal>> 1.23.hex()'0x1.3ae147ae147aep+0'#The difference between the built-in function hex and binascii.hexlify() is that #hex can only accept integers, not strings>> hex('88')Traceback (most recent call last): File ", line 1, in hex('88')TypeError: hex() argument can't be converted to hexbin():ba

bin(): convert decimal integer to binary character

#Convert decimal integers to binary>>> bin(88)'0b1011000'>> bin(33)'0b100001'oct(): Convert decimal to octal characters #Convert decimal to octal>> oct(500)'0764'>> oct(488)'0750'

chr(): converts an integer into a corresponding single character in the ASCII code table

#Convert an integer to the corresponding single character in the ASCII code table>>> chr(98)'b'>>> chr(97)'a'ord(): Contrary to chr, convert the characters in the ASCII code table to the corresponding integer>> ord ('b')98>> ord ('c ')99 At this point, the study of "how to use the binascio module in Python" is over, hoping to solve everyone's doubts. Theory and practice can better match to help everyone learn, go and try it! If you want to continue learning more relevant knowledge, please continue to pay attention to the website, Xiaobian will continue to strive to bring more practical articles for everyone!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report