简体   繁体   中英

Trying to understand binary conversion script

I have the following python code, which converts a binary string into plain-text:

import bin ascii
n = int('01100001011000100110001101100100', 2)
binascii.unhexlify('%x' % n)

This code functions properly, but I don't understand what's going on here.

  1. What is the purpose of the "2" at the end of the int declaration?
  2. What is the purpose of the "'%x' % n" argument?

What is the purpose of the 2 at the end of the int declaration?

According to the documentation , int can take a second argument: the base of the first argument.

What is the purpose of the '%x' % n argument?

%x in a string indicates that an item will be formatted to hexadecimal with lowercase letters. '%x' % n . It is similar to the builtin hex function, except it doesn't have the leading 0x in the string. An equivalent expression would be format(n, 'x') .

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM