char array to long

Need help with the long variable. I spent the last hours researching how to use it correctly, but no success. I am usually programming in assembly so I am no expert in C/C++. So here is my problem:

I have an OLED screen hooked up and the display and it is showing:

tempChars: 06528.2580
longDeg: 65
longMin: 217044

the first two lines are what I expect, but the 217044 puzzles me. I would expect this to be 282580.
Why can I not copy the characters into the long variable correctly? The method works with smaller variable types like the byte for the longDeg, but I can't seem to get the long variable filled with the correct values. Code is posted below.

I use Arduino 1.8.8 on PC. Just in case this is compiler related.

#include <Wire.h>
#include <U8g2lib.h>

//OLED - interface
U8G2_SSD1306_128X64_NONAME_1_HW_I2C u8g2(U8G2_R0, /* reset=*/ 
U8X8_PIN_NONE);

char tempChars[] = "06528.2580";

float longitude = 0.0;  
byte longDeg = 0; //degrees
long longMin = 0; // minutes

//------------------------------------------------------------------------------
void setup()   {
   u8g2.begin();  //start OLED
}
//------------------------------------------------------------------------------
void loop() {
               int i=0;
               while(i < 10){
                 if(i== 0) { } //
                 if(i == 1){ longDeg =  (tempChars[i] - '0')*10; } 
                 if(i == 2){ longDeg += (tempChars[i] - '0'); } 
                 if(i == 3){ longMin =  (tempChars[i] - '0')*100000; }     
                 if(i == 4){ longMin += (tempChars[i] - '0')*10000; } 
                 // i == 5 is decimal point                 
                 if(i == 6){ longMin += (tempChars[i] - '0')*1000; } 
                 if(i == 7){ longMin += (tempChars[i] - '0')*100; } 
                 if(i == 8){ longMin += (tempChars[i] - '0')*10; } 
                 if(i == 9){ longMin += (tempChars[i] - '0'); }    
                 i++;
               }
     drawScreen(); 
} // END loop

//------------------------------------------------------------------------------
void drawScreen(void){
    u8g2.firstPage();
    do {
      u8g2.setDrawColor(1); 
      u8g2.setFontPosTop(); 
      u8g2.setFont(u8g2_font_6x12_tr); 
    
      u8g2.setCursor(0, 10); u8g2.print("tempChars: "); u8g2.print(tempChars); 
      u8g2.setCursor(0, 20); u8g2.print("longDeg:    "); u8g2.print(longDeg);  
      u8g2.setCursor(0, 30); u8g2.print("longMin:      "); u8g2.print(longMin);      

   } while( u8g2.nextPage() );
} // END drawScreen

you are multiplying beyond the size of the variable

 if(i == 3){ longMin =  (tempChars[i] - '0')*100000; }     
if(i == 4){ longMin += (tempChars[i] - '0')*10000; }

for the first value the literal is a long, but for the second it needs to be explicitif(i == 4){ longMin += (tempChars[i] - '0')*10000L; } or the calculation will be done 16-bit

THANKS! Is there anywhere further documentation on this topic? I would have never been able to figure out that the L needs to go there.

Can you not simply print the tempChars string ?

If you really need to convert the tempChars string into a float then look to use the atof() function

Sure, printing the char array works fine. But I needed to do some math with it too. Hence the need for converting it into variables.
Is atof() faster than my method? I guess I would have to NUL terminate the char array for it to work, correct?

Is atof() faster than my method? I guess I would have to NUL terminate the char array for it to work, correct?

Faster, I don't know but neater certainly. Is speed important ? I would think that printing it on the screen would be a slow process

The array of chars needs to be terminated with a NULL for atof() to work but in your example program it already is terminated correctly

An example

char tempChars[] = "06528.2580";

void setup()
{
  Serial.begin(115200);
  float aFloat = atof(tempChars);
  Serial.println(aFloat, 4);
}

void loop()
{
}
1 Like