Convert 8 Byte timestamp to a human readable time!
16 次查看(过去 30 天)
显示 更早的评论
I created a Matlab script that reads a file and parses out data. One data field is 8 bytes and represents a timestamp in seconds.
I browsed around and asked a few questions and came up with the following code for reading the eight bytes and converting to a time in seconds: ("this_timestamp" is the full 8 bytes of time data, file is little-endian format)
message_timestamp = typecast(uint64(hex2dec([reshape(flipud(rot90(dec2hex(this_timestamp(8:-1:5)))),1,[]),reshape(flipud(rot90(dec2hex(this_timestamp(4:-1:1)))),1,[])])),'double');
This seems to work fine under most situations, (I get the times exactly as I know they should be), but in rare circumstances the times come out as "e-310". One case this happens in is when the 8 bytes are: "00 00 00 00 00 00 5d 40" which after converting should be 116.000000.
Is there something I'm doing (or not doing) that doesn't handle data with "0"s after the decimal?
0 个评论
采纳的回答
James Tursa
2015-9-23
编辑:James Tursa
2015-9-23
I haven't gone through your one-liner in any detail, but if you are starting with the 8 bytes shown above, then a simple typecast seems to work on my PC (little endian):
>> h = ['00';'00';'00';'00';'00';'00';'5d';'40'] % the hex values
h =
00
00
00
00
00
00
5d
40
>> this_timestamp = uint8(hex2dec(h)) % the equivalent 8 bytes
this_timestamp =
0
0
0
0
0
0
93
64
>> typecast(this_timestamp,'double')
ans =
116
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Type Conversion 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!