chore: initial commit

This commit is contained in:
你的用户名
2025-11-01 21:58:31 +08:00
commit 0406b5664f
101 changed files with 20458 additions and 0 deletions

37
utils/bytes_helper.py Normal file
View File

@@ -0,0 +1,37 @@
"""
Bytes处理工具 - 统一处理所有bytes和字符串转换
"""
from typing import Union, Optional
def bytes_to_hex(data: Optional[Union[bytes, str]]) -> Optional[str]:
"""bytes转hex字符串字符串保持原样"""
if data is None:
return None
if isinstance(data, bytes):
return data.hex()
return str(data)
def hex_to_bytes(hex_str: Optional[Union[str, bytes]]) -> Optional[bytes]:
"""hex字符串转bytes不可解析时返回原始字节"""
if hex_str is None:
return None
if isinstance(hex_str, bytes):
return hex_str
try:
return bytes.fromhex(hex_str)
except (ValueError, TypeError):
if isinstance(hex_str, str):
return hex_str.encode("utf-8")
return bytes(hex_str)
def safe_callback_data(callback_data: Optional[Union[str, bytes]]) -> Optional[str]:
"""安全处理callback_data统一转为hex便于存储"""
return bytes_to_hex(callback_data)
def restore_callback_data(hex_str: Optional[Union[str, bytes]]) -> Optional[bytes]:
"""恢复callback_data为bytes"""
return hex_to_bytes(hex_str)