1

I looked everywhere and can't find an answer to this specific question :(

I have a string date, which contains the date with all the special characters stripped away. (i.e : yyyymmddhhmm or 201212031204).

I'm trying to convert this string into an int to be able to sort them later. I tried atoi, did not work because the value is too high for the function. I tried streams, but it always returns -858993460 and I suspect this is because the string is too large too. I tried atol and atoll and they still dont give the right answer.

I'd rather not use boost since this is for a homework, I dont think i'd be allowed.

Am I out of options to convert a large string to an int ? Thank you!

What i'd like to be able to do :

int dateToInt(string date)
{
date = date.substr(6,4) + date.substr(3,2) + date.substr(0,2) + date.substr(11,2) + date.substr(14,2);
int d;
d = atoi(date.c_str());
return d;

}
4
  • 3
    If your only need is to sort them, sort them as strings. Commented Dec 3, 2012 at 17:14
  • 2
    Why don't you return it in date/time struct? There's no way to store that many digits in an int, so some larger data type is necessary; using the same form as system time functions seems appropriate (date_t iirc) Commented Dec 3, 2012 at 17:14
  • Do you have any 64-bit variable data types available to you? Your resulting number is over 201 billion which is far too large for a 32-bit integer to hold. The maximum for an unsigned 32-bit integer is 4,294,967,295. Commented Dec 3, 2012 at 17:15
  • @peachykeen, you're right, it seems like it'd make more sense to use a date/time struct for that. I'm just a little confused about how I'd be able to sort these structs after.. Commented Dec 3, 2012 at 17:21

3 Answers 3

5

You get negative numbers because 201212031204 is too large to fit int. Consider using long longs

BTW, You may sort strings as well.

Sign up to request clarification or add additional context in comments.

Comments

2

You're on the right track that the value is too large, but it's not just for those functions. It's too large for an int in general. ints only hold up to 32 bits, or a maximum value of 2147483647 (4294967295 if unsigned). A long long is guaranteed to be large enough for the numbers you're using. If you happen to be on a 64-bit system, a long will be too.

Now, if you use one of these larger integers, a stream should convert properly. Or, if you want to use a function to do it, have a look at atoll for a long long or atol for a long. (Although for better error checking, you should really consider strtoll or strtol.)

Completely alternatively, you could also use a time_t. They're integer types under the hood, so you can compare and sort them. And there's some nice functions for them in <ctime> (have a look at http://www.cplusplus.com/reference/ctime/).

1 Comment

And also you can define compare(void ,void) and use sort(a,compare) avalible in cstdlib
0
typedef long long S64;

S64 dateToInt(char * s) {
    S64 retval = 0;
    while (*s) {
         retval = retval * 10 + (*s - '0');
         ++s;
    }
    return retval;
}

Note that as has been stated, the numbers you're working with will not fit into 32 bits.

1 Comment

You need to increment s. The loop never ends

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.