我正在尝试使用,str.encode()但我得到了
>>> "hello".encode(hex)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: must be string, not builtin_function_or_method
Run Code Online (Sandbox Code Playgroud)
我尝试了很多变种,它们似乎都在Python 2.5.2中工作,所以我需要做些什么才能让它们在Python 3.1中工作?
我有数据存储在一个字节数组中.如何将此数据转换为十六进制字符串?
我的字节数组示例:
array_alpha = [ 133, 53, 234, 241 ]
Run Code Online (Sandbox Code Playgroud) 我使用的是Python 2.7.我有一个字母数字字符串,我想在其上执行加密/解密.无论我做什么都应该保持双向,结果也应该是字母数字.
例如:
str = 'ma6546fbd'
encrypted_data = encrypt_function(str)
decrypted_data = decrypt_function(encrypted_data)
print decrypted_data # I get 'ma6546fbd'
Run Code Online (Sandbox Code Playgroud)
我做了什么:
我写了一个函数
def xor_crypt_string(data, key):
return ''.join(chr(ord(x) ^ ord(y)) for (x,y) in izip(data, cycle(key)))
Run Code Online (Sandbox Code Playgroud)
这需要数据和一个键并返回结果,问题是它也包含特殊字符,我想避免.
我想转换b'\xc2\xa0\x38'成b'x38'python3.
b'\xc2\xa0\x38'.replace(u'\xc2\xa0',"")
b'\xc2\xa0\x38'.replace(u'\xc2a0',"")
TypeError: a bytes-like object is required, not 'str'
Run Code Online (Sandbox Code Playgroud)
在网页中,c2 a0表示NO-BREAK SPACE,其unicode点为U + 00A0.
Unicode code point character UTF-8 (hex.) name
U+00A0 c2 a0 NO-BREAK SPACE
Run Code Online (Sandbox Code Playgroud)
注意:c2a0是不可打印的,字符列在这里是空白的.
如何用replace方法转换b'\xc2\xa0\x38'成b'\x38'?
我有一个 base64 编码的随机数作为 24 个字符的字符串:
nonce = "azRzAi5rm1ry/l0drnz1vw=="
Run Code Online (Sandbox Code Playgroud)
我想要一个 16 字节的 int:
0x6b3473022e6b9b5af2fe5d1dae7cf5bf
Run Code Online (Sandbox Code Playgroud)
我最好的尝试:
from base64 import b64decode
b64decode(nonce)
>> b'k4s\x02.k\x9bZ\xf2\xfe]\x1d\xae|\xf5\xbf'
Run Code Online (Sandbox Code Playgroud)
如何从 base64 字符串中获取整数?
python ×5
string ×3
python-3.x ×2
base64 ×1
byte ×1
bytearray ×1
encode ×1
encryption ×1
hex ×1
python-2.7 ×1
replace ×1