我正在尝试将纪元日期时间转换为 python 中的字节数组 但它以 10 字节的形式出现,它应该以 4 字节的形式出现。
from time import time
curTime = int(time.time())
b = bytearray(str(curTime))
len(b) #comming as 10
谁能帮我解决一下
最佳答案
您正在转换时间戳的字符串表示形式,而不是整数。
你需要的是这个函数:
struct.pack_into(fmt, buffer, offset, v1, v2, ...) 它记录在 http://docs.python.org/library/struct.html靠近顶部。
import struct
from time import time
curTime = int(time())
b = struct.pack(">i", curTime)
len(b) # 4
关于python - 在 Python 中将 Epoch DateTime 转换为字节数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38183676/