How to convert array into 64bit Double Mac Absolut

2019-08-15 10:33发布

Is it possible to convert Array having binary or hex values stored as each element into 64bit Double Mac Absolute Time? When I inspect array using p var_bytes console shows following output.

\000\000\000\000\000\000\000\000\000\000\000\234\225x\266A\000\000\000\345\005\230\264

Is it possible to convert above array elements in 64bit Double Mac Absolute Time as a string?

My code is following just a simple do..end

puts "\nClose off the page header#{y.unpack("n")}\n"
            z.scan(/(.{8})(.{8})(.{4})(.{4})(.{4})(.{4})(.{8})(.{8})(.{8})(.*\w)/m).each do |j,k,l,m,n,o,p,q,r,s|
                puts "\nContent1#{j.unpack("n")}\n"
                puts "\nContent2:#{k.unpack("n")}\n"
                puts "\nContent3:#{l.unpack("n")}\n"
                puts "\nContent4:#{m.unpack("n")}\n"
                puts "\nContent5:#{n.unpack("n")}\n"
                puts "\nContent6:#{o.unpack("n")}\n"
                puts "\nContent7:#{p.unpack("n")}\n"
                expdt = Time.at((q.unpack("L"))[0])
                createdt = Time.at((r.unpack("L"))[0])
                puts "Date1:\n#{expdt}\n"
                puts "\nDate2:\n#{createdt}\n"
                puts "\nCookie:\n"
                puts s.split(/\0/m)
            end
        end

what will the simple way to convert this negative values to positive so Time.at wont give error and then convert it according to MAC Epoch time?

1条回答
一夜七次
2楼-- · 2019-08-15 10:48

Try this:

MAC_EPOCH = Time.gm(2001,1,1)

def bin2time(bin)
    return MAC_EPOCH + (bin.unpack "D")[0]
end

where bin is an 8-byte representation of a double precision float.

you may need to change "D" to "E" or "G" depending on where you are getting your data from. check the unpack docs for details.

查看更多
登录 后发表回答