I want to convert a very long string of numbers to a double in a portable way in C. In my case, portable means that it would work in Linux and Windows. My ultimate goal is to be able to pack a string of numbers into an 8-byte double and fwrite/fread to/from a binary file. The number is always unsigned.
I am using this string to pack a 4 digit year, 2 digit month, 2 digit day, 4 digit HH:MM, 1 digit variable, and a 10 digit value. So, trying to pack 23 bytes into 8 bytes.
I have tried all of the standard things:
char myNumAsString[] = "1234567890123456789";
char *ptr;
char dNumString[64];
double dNum;
dNum = atol(myNumAsString);
sprintf(dNumString, "%lf", dNum);
dNum = atof(myNumAsString);
sprintf(dNumString, "%lf", dNum);
dNum = strtod(myNumAsString, &ptr);
sprintf(dNumString, "%lf", dNum);
sscanf(myNumAsString, "%lf", &dNum);
sprintf(dNumString, "%lf", dNum);
And none of these work; they all round off the last several numbers. Any portable way to do this?
Take advantage that part of the string is a timestamp and not any set of digits.
With 60 minutes, 24 hours, 365.25 days/year,
y
years, a digit and 10 digits, there are60*24*365.25*y*10*pow(10,10)
combinations or about5.3e16 * y
An 8-byte, 64-bit number has
1.8e19
combinations. So if the range of years is 350 or less (like 1970 to 2320), things will fit.Assuming unix timestamp, and OP can convert a time string to
time_t
(check outmktime()
) ....Reverse to unpack.
Or a more complete portable packing (code untested)