質問

I want to convert a section of a char array to a double. For example I have:

char in_string[] = "4014.84954";

Say I want to convert the first 40 to a double with value 40.0. My code so far:

#include <stdio.h>
#include <stdlib.h> 

int main(int arg) {
  char in_string[] = "4014.84954";
  int i = 0;
  for(i = 0; i <= sizeof(in_string); i++) {
    printf("%c\n", in_string[i]);
    printf("%f\n", atof(&in_string[i]));
  }
}

In each loop atof it converts the char array from the starting pointer I supply all the way to the end of the array. The output is:

4
4014.849540
0
14.849540
1
14.849540
4
4.849540
.
0.849540
8
84954.000000   etc...

How can I convert just a portion of a char array to a double? This must by modular because my real input_string is much more complicated, but I will ensure that the char is a number 0-9.

役に立ちましたか?

解決

The following should work assuming:

I will ensure that the char is a number 0-9.

double toDouble(const char* s, int start, int stop) {
    unsigned long long int m = 1;
    double ret = 0;
    for (int i = stop; i >= start; i--) {
        ret += (s[i] - '0') * m;
        m *= 10;
    }
    return ret;
}

For example for the string 23487 the function will do this calculations:

ret = 0
ret += 7 * 1
ret += 8 * 10
ret += 4 * 100
ret += 3 * 1000
ret += 2 * 10000
ret = 23487

他のヒント

You can copy the desired amount of the string you want to another char array, null terminate it, and then convert it to a double. EG, if you want 2 digits, copy the 2 digits you want into a char array of length 3, ensuring the 3rd character is the null terminator.

Or if you don't want to make another char array, you can back up the (n+1)th char of the char array, replace it with a null terminator (ie 0x00), call atof, and then replace the null terminator with the backed up value. This will make atof stop parsing where you placed your null terminator.

Just use sscanf. Use the format "ld" and check for return value is one.

What about that, insert NULL at the right position and then revert it back to the original letter? This means you will manipulate the char array but you will revert it back to the original at the end.

You can create a function that will make the work in a temporary string (on the stack) and return the resulting double:

double atofn (char *src, int n) {
  char tmp[50]; // big enough to fit any double

  strncpy (tmp, src, n);
  tmp[n] = 0;
  return atof(tmp);
}

How much simpler could it get than sscanf?

#include <assert.h>
#include <stdio.h>

int main(void) {
    double foo;
    assert(sscanf("4014.84954", "%02lf", &foo) == 1);
    printf("Processed the first two bytes of input and got: %lf\n", foo);

    assert(sscanf("4014.84954" + 2, "%05lf", &foo) == 1);
    printf("Processed the next five bytes of input and got: %lf\n", foo);

    assert(sscanf("4014.84954" + 7, "%lf", &foo) == 1);
    printf("Processed the rest of the input and got: %lf\n", foo);
    return 0;
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top