Go Down

Topic: dtostrf (Read 534 times) previous topic - next topic

ccomparey

Hello,


I am trying to use BLE to transfer orientation from a BNO055 sensor data to an app on a tablet. My orientation data from the sensor has double data type but for programming of the app it is much easier to receive the 4 components (w,x,y,z)  as a string. At first I thought this would be easy using the dtostrf() function, however, now I am realizing that is only part avr library and will not work for my mkr wifi 1010. has anyone made or found a simple function to convert doubles to strings with this board?

Worded differently: I have 4 double variables and I want to convert them to a string with commas seperating each variable.

What I have
double w = 0.12
double x = -3.45
double y = 6.78
double z = -9.10

and I want to convert this to a string  "0.12,-3.45,6.78,-9.10"

I have done some looking around the forum without much luck, if you know a simple solution or where i can find one it would be greatly appreciated.

Thanks,
Chris

ccomparey

If anyone else is interested in this after about 5 more hours of digging i found this thread which at the end gives the following dtostrf function which seems to work so far.
Code: [Select]

char * dtostrf(double number, signed char width, unsigned char prec, char *s) {

    if (isnan(number)) {
        strcpy(s, "nan");
        return s;
    }
    if (isinf(number)) {
        strcpy(s, "inf");
        return s;
    }

    char* out = s;
   
    int fillme = width; // how many cells to fill for the integer part
    if (prec > 0) {
        fillme -= (prec+1);
    }
   
    // Handle negative numbers
    if (number < 0.0) {
        *out++ = '-';
        fillme--;
        number = -number;
    }

    // Round correctly so that print(1.999, 2) prints as "2.00"
    // I optimized out most of the divisions
    double rounding = 2.0;
    for (uint8_t i = 0; i < prec; ++i)
        rounding *= 10.0;     
    rounding = 1.0 / rounding;

    number += rounding;
   
    // Figure out how big our number really is
    double tenpow = 1.0;
    int digitcount = 1;
    while (number >= 10.0 * tenpow) {
        tenpow *= 10.0;   
        digitcount++;
    }
   
    number /= tenpow;
    fillme -= digitcount;
   
    // Pad unused cells with spaces
    while (fillme-- > 0) {
        *out++ = ' ';
    }
   
    // Print the digits, and if necessary, the decimal point
    digitcount += prec;
    int8_t digit = 0;
    while (digitcount-- > 0) {       
        digit = (int8_t)number;
        if (digit > 9) digit = 9; // insurance
        *out++ = (char)('0' | digit);
        if ((digitcount == prec) && (prec > 0)) {
            *out++ = '.';
        }   
        number -= digit;
        number *= 10.0;
    }

    // make sure the string is terminated
    *out = 0;
    return s;
}

pmalasp

dtostrf does not seem extremely popular, so I had my troubles and started to write my own, just in case someone else need it:

https://github.com/pmalasp/dtostrf


Go Up