1

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
1
  • One comment miles down says 'Oh, I made a typo: I really have "23:0"'. (a) Edit the question, and (b) In that case, if you are on POSIX machine, you could/should investigate strptime() to convert from string to time and strftime() to convert from time to string. However, it might be overkill for just hours and minutes. Commented Dec 24, 2010 at 7:51

3 Answers 3

2

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.

char *str = "23:0";
int hour, min;
sscanf(str, "%d:%d", &hour, &min);
/* hour = 23
   min  = 0
*/

After that you can do the math that you need and spit the results back out to a buffer.

char buf[100];
hour = (hour + 6) % 24;
snprintf(buf, 100, "%d:%d", hour, min);
Sign up to request clarification or add additional context in comments.

16 Comments

The question says '23.0' not '23:0'. Also, 23+6 = 5 (modulo 24).
@Jonathan: According to the comment in Santiago's Answer, it was supposed to be "23:0". So I'm going on that. As for the addition, I fixed that but the stupid draft didn't save and lost my change... easily fixed tho.
when I write my function I get an error "cannot convert 'string to char*' in initiliazation" my function looks like this. Code: 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
@user: In C, there is no true "String" type so it must have been a type you defined. How was it defined? p.s., put all the new code that you come up with as an edit into your question above. It's hard to read when put in a comment and you shouldn't be adding this information as "answers".
I have feedtime defined as a String my function takes that String and tries the char *str = x which gives me the error.
|
0

There are simple functions for these simple tasks in the standard library. Look up atoi() and atof() for string-to-number conversion, and sprintf() for number to string.

Edit: example. Code:

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

int main() {

 char string[10];
 int n1, n2, result;

 n1 = atoi("23");
 n2 = 6;

 result = (n1 + n2) % 24;

 sprintf(string, "%d", result);
 printf("Result: %s\n", string);

 return 0;
}

stdout:

Result: 5

Cheers!

1 Comment

I just realized I had a mistype. The string I have is not 23.0 but 23:0. Any ideas? For converting them, I don't believe theres a function for that since its not really a number...
0

Sounds like you need a few things done at once (even though this really sounds like a homework assignment). The very basic example would be:

char *x = "23.0";
char *y = "6.0";
float result = atof(x) + atof(y);
float result_24h = result % 24; // Modulo to get remainer only
char result_str[32]; // bad bad form, but good enough for this example
sprintf(result_str,"%f",result_24h);

At least something along those lines, written off the top of my head so apologies in advance for any typos/syntax errors;

1 Comment

strtod() or strtof() is usually preferred to atof(), as the latter is often referred to as "deprecated" on *nix systems (despite the C standard), and because the former functions have predictable behavior on inability to parse, as well as overflow/underflow.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.