Splitting an int into two ints in C

Discussion in 'Mac Programming' started by Cromulent, Dec 20, 2007.

  1. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #1
    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
     
  2. tkermit macrumors 68030

    tkermit

    Joined:
    Feb 20, 2004
    #2
    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)
     
  3. antibact1 macrumors 6502

    Joined:
    Jun 1, 2006
    #3
    firstDigits = (int)(year / 100);
    lastDigits = year % 100;

    Edit: beat to it :)
     
  4. therevolution macrumors 6502

    Joined:
    May 12, 2003
    #4
    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.
     
  5. Krevnik macrumors 68040

    Krevnik

    Joined:
    Sep 8, 2003
    #5
    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. ;)
     
  6. therevolution macrumors 6502

    Joined:
    May 12, 2003
    #6
    That's basically what I said, though perhaps not as succinctly. :p
     
  7. SC68Cal macrumors 68000

    Joined:
    Feb 23, 2006
    #7
    Doesn't strptime() already have these functions?
     
  8. Cromulent thread starter macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #8
    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.
     
  9. yeroen macrumors 6502a

    yeroen

    Joined:
    Mar 8, 2007
    Location:
    Cambridge, MA
    #9
    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)
     
  10. SC68Cal macrumors 68000

    Joined:
    Feb 23, 2006
    #10
    Read more carefully.

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

    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.
     
  11. Cromulent thread starter macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #11
    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.
     
  12. ChrisA macrumors G4

    Joined:
    Jan 5, 2006
    Location:
    Redondo Beach, California
    #12
    Skips the books. Just type "man strptime" at the terminal to get the definitive answer.
     
  13. aross99 macrumors 68000

    aross99

    Joined:
    Dec 17, 2006
    Location:
    East Lansing, MI
    #13
    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?
     
  14. Cromulent thread starter macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #14
    I've just been investigating that possibility oddly.
     

Share This Page