Split char without delimiter and convert to int

Hi guys,
I have a char in this format:

20150224172717.000

Is a date + time: date 2015 02 24 time 17 27 17

I need to split this char and convert it into a multiple int.
Int like: year, month, day, hour, minute, second.

I don’t need the part “.000”.

But I don’t know exactly how proceed.

Because there aren’t a delimiter in the string.
I think that I need a for cycle, but I don’t know exactly how do.

Thank you

I have a char in this format:

Nonsense. A char is ONE character.

But I don't know exactly how proceed.

Start by getting the terminology straight, and by showing some code.

Does this look interesting?

void setup()
{
Serial.begin(57600);
char example[]={"20150224172717.000"};
int year    = valueFromString( example, 0, 4);
int month   = valueFromString( example,4,2);
int day     = valueFromString ( example, 6, 2);
int hour    = valueFromString ( example, 8, 2);
int minute  = valueFromString ( example, 10, 2);
int second  = valueFromString ( example, 12, 2);

Serial.print( "Year ");
Serial.println (year);
Serial.print( "Month ");
Serial.println (month);
Serial.print( "Day ");
Serial.println (day);
Serial.print( "Hour ");
Serial.println (hour);
Serial.print( "minute ");
Serial.println (minute);
Serial.print( "Second ");
Serial.println (second);

}

void loop()
{
  
}

int valueFromString(char* string,int start, int width)
{
int value=0;
for( int n=0; n < width; n++ )
  value = value * 10 + string[start +n] - '0'; 

return value;  
}

anerDev: Hi guys, I have a char in this format:

Is a date + time: date 2015 02 24 time 17 27 17

I need to split this char and convert it into a multiple int. Int like: year, month, day, hour, minute, second.

I don't need the part ".000".

But I don't know exactly how proceed.

Because there aren't a delimiter in the string. I think that I need a for cycle, but I don't know exactly how do.

Thank you

maybe you have a char array (string, C string, char*) so let's get semantics aside and get to the issue!

there are lots of functions that can help you smash this into the bits you want to get to...

look into:

strtok() to bust up the string with a delimiter, like your "."

strchr() to search for a character

atoi(): converts a C string to an int

the rest can be handled easily once you have your integer less the trailing period and zeros, I would think.

(or just use Ken's function!!)

anerDev: I have a char in this format:

Is a date + time: date 2015 02 24 time 17 27 17

I need to split this char and convert it into a multiple int. Int like: year, month, day, hour, minute, second.

I don't need the part ".000".

But I don't know exactly how proceed.

Because there aren't a delimiter in the string. I think that I need a for cycle, but I don't know exactly how do.

Here is a demo that uses "sscanf" to cut the string into integers and reformats using "snprintf":

void setup() 
{
  Serial.begin(9600);
  char tmp[]="20150224172717.000"; // original string
  Serial.print("Original: ");Serial.println(tmp);
  int year, month, day, hour, minute, second; // some int variables
  // cut original string into pieces
  sscanf(tmp,"%04d%02d%02d%02d%02d%02d",&year,&month,&day,&hour,&minute,&second);
  char strbuf[51]; // new string buffer
  // new formatting
  snprintf(strbuf,sizeof(strbuf),"German date: %02d.%02d.%04d  Time ==> %02d:%02d:%02d",day,month,year,hour,minute,second);
  Serial.println(strbuf);
}

void loop(){}

Using 'sscanf' the cutting into integers is a one-liner.

Hi guys, sorry for this delay !

All code work perfect. Thank you so much =D

EDIT:

I make a simple gists for the 2 methods:

https://gist.github.com/anerDev/b5bf703143ea1d0342e7 https://gist.github.com/anerDev/0fd1cf27a8d28e68df8a

Hi guys, again ...

There is a problem; I opened a topic: http://forum.arduino.cc/index.php?topic=311126.new

Can you read ?

thank you

I'm not a big fan of the sprintf family of functions, primarily because they are often an H-bomb-to-kill-an-ant solultion to a problem. KenF and jurs both provided solutions that are functionally about the same. However, KenF's solution is 2872 bytes while jurs is 5656 bytes. To me, the small additional effort to implement KenF's approach is worth it to save that much memory.

anerDev: There is a problem; I opened a topic: http://forum.arduino.cc/index.php?topic=311126.new

Can you read ?

If you run into trouble with any of the solutions (both of them working, you tested it!) while including them into a sketch that includes a whole bunch of third-party and Arduino libraries, you most likely run out of RAM memory.

First thing I'd do: Check the amount of free RAM.

If you run out of RAM, you'd either have to think about other programming techniques than to copy-and-paste libraries and third-party code, or you'd have to use a controller that provides more RAM.

Thank you guys ... I thought that the problem was the RAM ... But is possible that affect only this part of the code and not all sketch ?

I have code to analyze a GPS string. I was able to do this task using only strlcpy() and atoi(). Very easy using those.

That format almost looks like ANSI, but the ANSI standard has a 'T' in there:

20150224T172717.000

@aarg can you give me ? :slight_smile:
@PaulMurrayCbr what’s this ?

Guys,
this is the output of avr-size:

AVR Memory Usage

Device: atmega2560

Program: 33898 bytes (12.9% Full)
(.text + .data + .bootloader)

Data: 3022 bytes (36.9% Full)
(.data + .bss + .noinit)

Is possibile that the RAM will go in saturation ? O.0
Thank you

This code extracts a 3 digit number from a string of numbers in msg_lat and stores the result in integer Lat.

          strlcpy (latDegrees, msg_lat, 3);
          Lat = atoi(latDegrees);

You have to consult the avr-gcc manual for documentation on such things. It's shipped with the IDE, but for some reason is "buried" in the program folder. You can download it elsewhere if you like.

@aarg tomorrow I will try .. Thank you