Conversion from unix time string to uint64

조회 수: 21 (최근 30일)
Hafiz Luqman
Hafiz Luqman 2019년 2월 20일
댓글: Jan 2019년 2월 25일
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