gpt4 book ai didi

python - struct.pack 在 ruby​​ 中等效吗?

转载 作者:行者123 更新时间:2023-12-01 09:32:03 25 4
gpt4 key购买 nike

我正在尝试将 python 代码转换为 ruby​​,但是我没有得到相同的值

python :

timestamp = 1523966261 #int(time.time())
math = timestamp // 30
time_buffer = struct.pack('>Q',math)

output : b'\x00\x00\x00\x00\x03\x07!\x1b'

ruby :

timestamp =  1523966261 # Time.new.to_i
math = timestamp / 30
time_buffer =[math].pack('Q>')

output : \x00\x00\x00\x00\x03\a!\e

我哪里出错了?

最佳答案

在 Ruby 中,标志是在类型之后指定的:

timestamp =  1523966261
math = timestamp / 30
time_buffer = [math].pack('Q>')
#=> "\x00\x00\x00\x00\x03\a!\e"

请注意:

"\a" == "\x07" #=> true
"\e" == "\x1b" #=> true

关于python - struct.pack 在 ruby​​ 中等效吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49879442/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com