avatar

Python进制转换

date: 2017-01-02 00:00:00
tags: [python,bytes_conversion]
categories:PYTHON


十进制,二进制转换

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
def convert_md5(code,byte_n=8):
"""
128位二进制字符串特征值转list,指定字节数
:param origin: "10100011011100101100101001100010101101111101101100101010100011001001011100100000011111011111110110001100111000010101001010110001"
:return: [ 163, 114, 202, 98, 183, 219, 42, 140, 151, 32, 125, 253, 140, 225, 82, 177 ]
"""
result = [int(code[i:i + byte_n], 2) for i in range(len(code)) if i % byte_n == 0]
return result

def int2bin(n, count=24):
"""
returns the binary of integer n, using count number of digits
"""
return "".join([str((n >> y) & 1) for y in range(count - 1, -1, -1)])

def unconvert_md5(origin,byte_n=8):
"""
特征列表转化为128字符串
:param origin: [ 163, 114, 202, 98, 183, 219, 42, 140, 151, 32, 125, 253, 140, 225, 82, 177 ]
:return: '10100011011100101100101001100010101101111101101100101010100011001001011100100000011111011111110110001100111000010101001010110001'
"""
return "".join([str(int2bin(i, 8)) for i in origin])
文章作者: luochenxi
文章链接: https://luochenxi.github.io/2019/07/19/yuque/Python%E8%BF%9B%E5%88%B6%E8%BD%AC%E6%8D%A2/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 Kirio

评论