我试图在 python 中将纪元日期时间转换为字节数组,但它是 10 字节,它应该是 4 字节。
from time import time
curTime = int(time.time())
b = bytearray(str(curTime))
len(b) #comming as 10
Run Code Online (Sandbox Code Playgroud)
任何人都可以帮助我出错的地方
您正在转换时间戳的字符串表示形式,而不是整数。
你需要的是这个功能:
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
Run Code Online (Sandbox Code Playgroud)
从这里被盗:https : //stackoverflow.com/a/7921876/2442434