繁体   English   中英

在Python中将Epoch DateTime转换为字节数组

[英]Converting Epoch DateTime to Byte Array in Python

我正在尝试将epoch datetime转换为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

从这里被盗: https : //stackoverflow.com/a/7921876/2442434

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM