Conversion from unix time string to uint64

8 次查看(过去 30 天)
I have lidar sensor data and every file is named on its timestamps. The file name 25 characters long (21 characters for time stamps and 4 characters for extension).
For example first file name is "1520944184833390730.pcd"
When I try to convert it to uint64
timeNum = uint64(str2num(fileNames(1:end-4)));
It gives
timeNum = 1520944184833390848
  2 个评论
Jan
Jan 2019-2-20
编辑:Jan 2019-2-20
Correctly. What is your question? You cannot store 25 digits exactly in an uint64. 2^64 is about 10^19.26, so you can expect 19 valid digits only. But as long as you do not mention, what your question is, it is not clear, how to help you.
Maybe the first 16 digits are the seconds, and the last 9 the nanoseconds?
Geoff Hayes
Geoff Hayes 2019-2-20
编辑:Geoff Hayes 2019-2-20
Hafiz - what does
str2num(fileNames(1:end-4))
return? Perhaps you are losing some precision with str2num... What happens if you use str2double instead?
In your above example, there are 19 digits in the name (less the extension) but you say The file name 25 characters long (21 characters for time stamps and 4 characters for extension). Which is correct?

请先登录,再进行评论。

采纳的回答

Jan
Jan 2019-2-21
The conversion by str2double creates a double, which uses 53 bits only. With sscanf you can get 64 significant bits to store the value:
format long g
a = uint64(str2num('123456789012345678'))
b = sscanf('123456789012345678', '%lu')
>> a = 123456789012345680 % Last 2 digits rounded as expected
>> b = 123456789012345678 % Accurate
With uint64 you can store 19 significant digits: log10(2^64 - 1) = 19.26
You did not mention yet, which problem you want to solve. What do you want to do with the string '1520944184833390730'?

更多回答(2 个)

Peter Perkins
Peter Perkins 2019-2-21
I'm gonna run with Jan's guess, which, in the absence of any description, seems plausible:
>> datetime(1520944184833390730/1e9,'convertFrom','posixtime')
ans =
datetime
13-Mar-2018 12:29:44
Lets say you have that character string.
>> t = '1520944184833390730.pcd';
>> secs = str2num(t(1:10))
secs =
1520944184
>> ns = str2num(t(11:19))
ns =
833390730
>> d = datetime(secs,'ConvertFrom','posixtime','Format','dd-MMM-yyyy HH:mm:ss.SSSSSSSSS') + milliseconds(ns/1e6)
d =
datetime
13-Mar-2018 12:29:44.833390730

Hafiz Luqman
Hafiz Luqman 2019-2-25
编辑:Hafiz Luqman 2019-2-25
Jan method works fine. I also figured it out by this way.
a = str2num(['uint64('123456789012345678');
  1 个评论
Jan
Jan 2019-2-25
I'm not sure, what you mean. The current syntax is not valid and uint64('12345') does not create the variable 12345 as UINT64.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Data Type Conversion 的更多信息

产品


版本

R2018b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by