简体   繁体   English

试图理解二进制转换脚本

[英]Trying to understand binary conversion script

I have the following python code, which converts a binary string into plain-text:我有以下 python 代码,它将二进制字符串转换为纯文本:

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? int 声明末尾的“2”的目的是什么?
  2. What is the purpose of the "'%x' % n" argument? “'%x' % n”参数的目的是什么?

What is the purpose of the 2 at the end of the int declaration? int 声明末尾的2的目的是什么?

According to the documentation , int can take a second argument: the base of the first argument.根据文档int可以采用第二个参数:第一个参数的基数。

What is the purpose of the '%x' % n argument? '%x' % n参数的目的是什么?

%x in a string indicates that an item will be formatted to hexadecimal with lowercase letters.字符串中的%x 表示项目将被格式化为带有小写字母的十六进制 '%x' % n . '%x' % n It is similar to the builtin hex function, except it doesn't have the leading 0x in the string.它类似于内置的hex函数,只是它在字符串中没有前导0x An equivalent expression would be format(n, 'x') .等效的表达式是format(n, 'x')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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