Base58 编码器 / 解码器
将文本编码为Base58或解码Base58字符串。用于比特币地址和IPFS CID。
常见问题
代码实现
import base58 # pip install base58
# Encode text to Base58
text = "Hello, World!"
encoded = base58.b58encode(text.encode()).decode()
print(encoded) # JxF12TrwUP45BMd
# Decode Base58 back to text
decoded = base58.b58decode(encoded).decode()
print(decoded) # Hello, World!
# Manual implementation without library
ALPHABET = b"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
def encode(data: bytes) -> str:
num = int.from_bytes(data, "big")
result = b""
while num > 0:
num, rem = divmod(num, 58)
result = bytes([ALPHABET[rem]]) + result
pad = len(data) - len(data.lstrip(b"\x00"))
return (b"1" * pad + result).decode()Comments & Feedback
Comments are powered by Giscus. Sign in with GitHub to leave a comment.