将char * []转换为double

时间:2018-02-22 16:00:59

标签: c arduino-ide

我有一个arduino代码,以char数据类型输入卷,我想将其转换为double,我尝试使用atof但它不会工作。是什么问题来自我的代码。对不起新手在这里!!

double v;
int data_count = 0;
int i;
char* Data[]={};
char* volume[]={};
char* inputTime[]={};

// Define the Keymap for Keypad
char keys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'.', '0', '#', 'D'}
};
lcd.setCursor(0, 0);
  lcd.print("Input Volume:");

  if (customKey != NO_KEY && customKey != '#'
      && customKey != 'A' && customKey != 'B' && customKey != 'C'
      && customKey != 'D') {
    Data[data_count] = customKey;
    lcd.setCursor(data_count, 1);
    lcd.print(Data[data_count]);
    data_count++;
  }

  if (customKey == '#') {
    volumelength = data_count;
    for (i = 0; i < volumelength; i++) {
      volume[i] = Data[i];          
    }
    clearData();
    nextstate();
    data_count = 0;
    v = atof(volume);
  }

1 个答案:

答案 0 :(得分:0)

<\ n>在arduino语言中有toFloat()https://www.arduino.cc/reference/en/language/variables/data-types/string/functions/tofloat/

其他atof()应该像 avr-gcc stdlib.h库中的8位AVR CPU一样工作,就像在arduino上一样

https://www.nongnu.org/avr-libc/user-manual/group__avr__stdlib.html):

#include <stdlib.h>

char s[] = "11.248"
double f = 0;

f=atof(s);

另见 http://www.ethernut.de/nutwiki/index.php/Converting_Strings_to_Floating_Point_Values (以太网用于8位CPU)

可能检查Data的内容是否真的是浮动......

avr-gcc / arduino中的另一个atof示例:https://forum.arduino.cc/index.php?topic=98595.0