I have a String for example "23:0" which is a hour format. I need to convert this to a int so I can add time to it for example. I have a string "23:0" I need to add 6 hours which would be "6:0" which would then give me "5:0", and then convert this back to a string.
Any ideas would be greatly appreciated 🙂
When I write my function I get an error “cannot convert ‘string to char*’ in initiliazation” my function looks like this:
int convert(String x){
char *str = x;
int hour; int minute;
sscanf(str, "%d:%d", &hour, &minute);
return hour;
}
convert(time) //time is a String for example 23:0
Since the string is in a particular format ([hour]:[min]), you can use
sscanf()to scan the string. Since the string is in an expected format, this would be the easiest to do. Otherwise you’d use the other methods described by everyone else.After that you can do the math that you need and spit the results back out to a buffer.