I am dealing with a sensor that timestamps the outgoing data using a string of characters in this format:
yyyymmddhhnnssW or yyyymmddhhnnssS depending on daylight savings setting.
Disregarding the W/S specifier I have a 14 character long numeric string, which I save into a long long (64 bit) variable since it will not fit into a long int.
Then later I need to make a MQTT packet of this value and now I have failed in finding any C/C++ conversion function that can give me a string representation of this long long (or uint64) value.
I have tried:
String LongLongToString(uint64 value)
{
char buf[25];
memset(buf, 0, sizeof(buf));
long lo = value % 10000000;
long hi = value / 10000000;
snprintf(buf, sizeof(buf), "%0.7l%0.7l", hi, lo);
return String(buf);
}
But it returns these warnings:
src\utils.cpp: In function 'String LongLongToString(uint64)':
src\utils.cpp:83:49: warning: conversion lacks type at end of format [-Wformat=]
snprintf(buf, sizeof(buf), "%0.7l%0.7l", hi, lo);
^
src\utils.cpp:83:49: warning: conversion lacks type at end of format [-Wformat=]
src\utils.cpp:83:49: warning: too many arguments for format [-Wformat-extra-args]
src\utils.cpp:83:49: warning: conversion lacks type at end of format [-Wformat=]
src\utils.cpp:83:49: warning: conversion lacks type at end of format [-Wformat=]
src\utils.cpp:83:49: warning: too many arguments for format [-Wformat-extra-args]
[quote="BosseB, post:1, topic:1053377"]
sensor that timestamps the outgoing data using a string of characters ...
I save into a long long (64 bit) variable[/quote]
why not just handle it as a string of characters (or bytes)? wouldn't the S attributes also work with that approach?
Because all of the data returned from the device are converted into global long values and the timestamp is much longer so it does not fit.
And this part is not written by me but downloaded from GitHub, I am just adding extra database storage functions etc...
And the timestamp was not handled in the original sketch so I figured I should just add one more item in the same way as all of the others.
But I assume I could make a special case and separate them into yyyymmdd in one long and hhnnss in another long, this way I would have to just concatenate two items that can be handled in the "normal" way all other values are processed.
I assume by that you mean the data consists of ASCII characters that represent single digits with a final W / S at the end? If that's the case, why in the world are your shoving it into a uint64_t? Use an array of char and parse it after the complete string is received.
the max value of a uint64_t is 18,446,744,073,709,551,615
so dividing by 10 million gives 1,844,674,407,370 which is way above what a 32 bit unsigned int can represent as the max value of a uint64_t is 4,294,967,295
Back on this again...
I have tried to simplify things by splitting the timestamp into two long variables:
HANTIMESTAMP_DAY and HANTIMESTAMP_TIME each will be set to a value like 221114 and 131447 for date and time.
They are declared like this:
long HANTIMESTAMP_DAY;
long HANTIMESTAMP_TIME;
Now I need to combine these into a value string for mqtt and tried this: