Files
telegram-customer-bot/utils/bytes_helper.py
2025-11-01 21:58:31 +08:00

38 lines
1.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""
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)