user3548808 user3548808 - 2 months ago 16
C++ Question

Linux c++ Convert seconds (double) to miliseconds, microseconds, nanoseconds, picoseconds

I have device which gives me delay in seconds (

), value like
, I have to convert that value to milliseconds, microseconds, nanoseconds, or picoseconds.
I wrote an application, in qt-creator in linux (c++).
I tried to use library
, but as I see it is keeping only long values, for each type and I always loose some data.
Which method is the best to do this?


Convert the char* to a std::string. Check that there are exactly 12 digits after the decimal point. Convert those to a unsigned long long number of picoseconds with std::strtoull(digits,10); (remember to specify the base explicitly, otherwise the leading zero will make it think it is an octal number).

If you need to handle delays >= 1s, convert the digits before the decimal point to a number of seconds in the same way and add them into the picoseconds with pico += seconds*(1000ull*1000*1000*1000);

Finally, nanoseconds = (picoseconds + 500)/1000;