编写字节而无需填充

发布于 2025-02-13 03:29:33 字数 1121 浏览 0 评论 0原文

鉴于这两个数字(自Unix时期以来毫秒),1656773855233和1656773888716,我正在尝试将它们写入二进制对象。踢者是我想将它们写入11个字节对象。

如果我接受时间戳只能在2039年9月7日之前代表日期,则这些数字应分别适合41位。 2 41位数字适合88位(11比),超过6位。

我已经尝试了以下Python代码,作为实现这一目标的(相当天真的)方法:

receive_time=1656773855233
event_time=1656773888716

with open("test.bin", "wb") as f:
    f.write(receive_time.to_bytes(6, "big"))
    f.write(event_time.to_bytes(6, "big"))

print(f"{receive_time:08b}")
print(f"{event_time:08b}")

with open("test.bin", "rb") as f:
    a = f.read()
    print(" ".join(f"{byte:08b}" for byte in a))

该代码的输出非常清楚地表明test.bin文件为12个字节,因为每个时间戳都是填充的将7 0转换为字节时。

我将如何将时间戳写入test.bin而无需单独填充它们?

The end result should be something like 0000001100000011011111101101010110010000000000111000000110111111011010110100101011001100 or 3037ED5900381BF6B4ACC

EDIT: Question originally mentioned that a 41bit number could only represent dates before May 15th, 2109 if that number自Unix时期以来代表毫秒。正如Interjay正确指出的那样,这是错误的,41位数字仅代表69,7年,因此最大日期将是2039年9月7日。但是,一个42位数字可以代表一个代表日期的数字,该号码在2109年5月15日之前日期。

Given these 2 numbers (milliseconds since Unix epoch), 1656773855233 and 1656773888716, I'm trying to write them to a binary object. The kicker is that I want to write them to a 11 byte object.

If I accept that the timestamps can only represent dates before September 7th 2039, the numbers should each fit within 41bits. 2 41bit numbers fits within 88bit (11bytes) with 6 bits in excess.

I've tried the following python code, as a (rather naive) way to achieve this:

receive_time=1656773855233
event_time=1656773888716

with open("test.bin", "wb") as f:
    f.write(receive_time.to_bytes(6, "big"))
    f.write(event_time.to_bytes(6, "big"))

print(f"{receive_time:08b}")
print(f"{event_time:08b}")

with open("test.bin", "rb") as f:
    a = f.read()
    print(" ".join(f"{byte:08b}" for byte in a))

The output of the code very clearly shows that the test.bin file is 12 bytes, because each timestamp is padded with 7 0's when converted to bytes.

How would I go about writing the timestamps to test.bin without padding them individually?

The end result should be something like 0000001100000011011111101101010110010000000000111000000110111111011010110100101011001100 or 3037ED5900381BF6B4ACC

EDIT: Question originally mentioned that a 41bit number could only represent dates before May 15th, 2109 if that number represented milliseconds since the Unix epoch. That is wrong, as interjay correctly pointed out, a 41bit number would only represent 69,7 years and the max date would thus be September 7th, 2039. A 42bit number, however, can represent a number that represents dates before May 15th, 2109.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

来日方长 2025-02-20 03:29:33

在将其转换为bytes

x = (receive_time << 41) + event_time
b = x.to_bytes(11, 'big')
with open("test.bin", "wb") as f:
    f.write(b)

Use a bit shift to combine both times into the same int, before converting that to bytes:

x = (receive_time << 41) + event_time
b = x.to_bytes(11, 'big')
with open("test.bin", "wb") as f:
    f.write(b)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文