gpt4 book ai didi

python - 为什么 binascii.b2a_base64() 和 base64.b64encode() 之间存在差异?

转载 作者:行者123 更新时间:2023-11-28 16:39:47 24 4
gpt4 key购买 nike

我试图理解我在以下两个函数中看到的一些不同行为:

def hex_to_64(string):
hex_string = binascii.a2b_hex(string)
return binascii.b2a_base64(hex_string)

def hex_to_64_2(string):
hex_string = binascii.a2b_hex(string)
return base64.b64encode(hex_string)

如果我将一个十六进制字符串传递给前者,我会在最后返回一个换行符,而后者则没有。这是有原因的吗?

最佳答案

没什么特别的,实现者决定这样做。它记录在 binascii module .

Convert binary data to a line of ASCII characters in base64 coding. The return value is the converted line, including a newline char. The length of data should be at most 57 to adhere to the base64 standard.

如果您对正确的剥离感到不舒服:

hex_to_64('aa').rstrip('\n')
>>>'qg=='

希望这对您有所帮助!

关于python - 为什么 binascii.b2a_base64() 和 base64.b64encode() 之间存在差异?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20927325/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com