Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Cromulent

macrumors 604
Original poster
Oct 2, 2006
6,810
1,100
The Land of Hope and Glory
I need to split an int into two ints. I have the year which I got from strptime() and need to get the first two digits into one int and the last two digits into another int. What is the method for doing so?

So year = 2007
firstDigits = 20
lastDigits = 07
 

tkermit

macrumors 68040
Feb 20, 2004
3,586
2,921
My thinking possibly being too simple I'd suggest the following:

Code:
int year;
int firstTwoDigits;
int LastTwoDigits;


//Integer-division !
firstTwoDigits = year/100; 

//modulo
LastTwoDigits = year%100;


(You'd possibly have to add a 0 in front if your digits are <10)
 

antibact1

macrumors 6502
Jun 1, 2006
334
0
I need to split an int into two ints. I have the year which I got from strptime() and need to get the first two digits into one int and the last two digits into another int. What is the method for doing so?

So year = 2007
firstDigits = 20
lastDigits = 07

firstDigits = (int)(year / 100);
lastDigits = year % 100;

Edit: beat to it :)
 

therevolution

macrumors 6502
May 12, 2003
468
0
The above mentioned solutions will work if you don't mind '07' being treated the same as '7'. But if you need to preserve the leading '0' then you're looking at some extra work somehow. Just something to keep in mind. It really depends on what you plan to do with the results.
 

Krevnik

macrumors 601
Sep 8, 2003
4,101
1,312
The above mentioned solutions will work if you don't mind '07' being treated the same as '7'. But if you need to preserve the leading '0' then you're looking at some extra work somehow. Just something to keep in mind. It really depends on what you plan to do with the results.

As long as it is an int, leading zeroes mean nothing. If you want to output the int with a leading zero, that requires some basic formatting magic. ;)
 

yeroen

macrumors 6502a
Mar 8, 2007
944
2
Cambridge, MA
Bah I feel like an idiot now :). Thanks for the help.

As for whether strptime() already having those functions I have no idea. I'm trying to learn different parts of the standard library using http://www.gnu.org/software/libc/manual/html_node/index.html and it is not the easiest document to read for a beginner.

In which case have you checked out Stevens APUE book?

http://www.amazon.co.uk/Programming...=sr_1_1?ie=UTF8&s=books&qid=1198257762&sr=8-1

It is, essentially, a lucid book length treatment of standard C library in UNIX (it's colloquially referred to as 'the bible' around work)
 

SC68Cal

macrumors 68000
Feb 23, 2006
1,642
0
Read more carefully.

http://www.gnu.org/software/libc/ma...g-Calendar-Time.html#Formatting-Calendar-Time

%C
The century of the year. This is equivalent to the greatest integer not greater than the year divided by 100.
This format was first standardized by POSIX.2-1992 and by ISO C99.

%Y
The year as a decimal number, using the Gregorian calendar. Years before the year 1 are numbered 0, -1, and so on.

You've got to learn to read documentation. Naturally, I'm not a fan of the GNU manual because it's meant to be read like a book, rather than being like an O'Reily quick reference.
 

Cromulent

macrumors 604
Original poster
Oct 2, 2006
6,810
1,100
The Land of Hope and Glory
You've got to learn to read documentation. Naturally, I'm not a fan of the GNU manual because it's meant to be read like a book, rather than being like an O'Reily quick reference.

I was under the impression that you had to use the flags appropriate to the input given. I'm already using the %Y flag by the way.

Now given the input (which I do not want to change the format of) and the purpose of the program which is to tell the user which day of the week any given date is equal too I'm not sure that method is applicable. Please let me know if it is though.
 

ChrisA

macrumors G5
Jan 5, 2006
12,913
2,160
Redondo Beach, California

aross99

macrumors 68000
Dec 17, 2006
1,541
1
East Lansing, MI
Any reason why you couldn't use sprintf() to convert the number to a string (with the leading zeroes), and then use string functions to break it apart?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.