Q: Get x digits from int

Discussion in 'C Programming' started by Michael Frederick, May 14, 2007.

  1. Hi,

    I'm having a bit of dippy here, can anyone help?

    How do I extract x digits from an Integer.

    For example int iTime = 101237
    I need to make it into a string in the format "10:12:37" therefore if
    I could get "10" "12" and "37" all would be well. I initially thought
    I could do it with a mask (binary and) but to no avail.

    Can someone put me out of my misery?
     
    Michael Frederick, May 14, 2007
    #1
    1. Advertising

  2. Op Mon, 14 May 2007 14:12:07 +0200 schreef Michael Frederick
    <>:
    > I'm having a bit of dippy here, can anyone help?
    >
    > How do I extract x digits from an Integer.
    >
    > For example int iTime = 101237
    > I need to make it into a string in the format "10:12:37" therefore if
    > I could get "10" "12" and "37" all would be well. I initially thought
    > I could do it with a mask (binary and) but to no avail.


    Decimals are not binary, so you cannot binary mask them.

    #include <stdio.h>
    char str[9];
    sprintf (str, "%06d", iTime);
    /* src 012345..\0 */
    /* dst 01:23:45\0 */
    str[7] = str[5];
    str[6] = str[4];
    str[4] = str[3];
    str[3] = str[2];
    str[5] = str[2] = ':';

    Voila!

    > Can someone put me out of my misery?


    That can be arranged as well.


    --
    Gemaakt met Opera's revolutionaire e-mailprogramma:
    http://www.opera.com/mail/
     
    Boudewijn Dijkstra, May 14, 2007
    #2
    1. Advertising

  3. Michael Frederick

    Lew Pitcher Guest

    On May 14, 8:12 am, Michael Frederick <> wrote:
    > Hi,
    >
    > I'm having a bit of dippy here, can anyone help?
    >
    > How do I extract x digits from an Integer.
    >
    > For example int iTime = 101237
    > I need to make it into a string in the format "10:12:37" therefore if
    > I could get "10" "12" and "37" all would be well. I initially thought
    > I could do it with a mask (binary and) but to no avail.


    int iTime = 101237,
    iHour, iMinute, iSecond;

    iSecond = iTime % 100;
    iMinute = (iTime/100) % 100;
    iHour = iTime / 10000;

    HTH
    --
    Lew
     
    Lew Pitcher, May 14, 2007
    #3
  4. Boudewijn Dijkstra wrote:

    > Op Mon, 14 May 2007 14:12:07 +0200 schreef Michael Frederick
    > <>:
    >> I'm having a bit of dippy here, can anyone help?
    >>
    >> How do I extract x digits from an Integer.
    >>
    >> For example int iTime = 101237
    >> I need to make it into a string in the format "10:12:37" therefore if
    >> I could get "10" "12" and "37" all would be well. I initially thought
    >> I could do it with a mask (binary and) but to no avail.

    >
    > Decimals are not binary, so you cannot binary mask them.
    >
    > #include <stdio.h>
    > char str[9];
    > sprintf (str, "%06d", iTime);
    > /* src 012345..\0 */
    > /* dst 01:23:45\0 */
    > str[7] = str[5];
    > str[6] = str[4];
    > str[4] = str[3];
    > str[3] = str[2];
    > str[5] = str[2] = ':';
    >
    > Voila!


    Just for the sake of it, here is an alternative.

    #include <stdio.h>
    char str[9];
    sprintf(str, "%02d:%02d:%02d", iTime/10000, (iTime/100)%100, iTime%100);

    Done.

    Bart v Ingen Schenau
    --
    a.c.l.l.c-c++ FAQ: http://www.comeaucomputing.com/learn/faq
    c.l.c FAQ: http://www.eskimo.com/~scs/C-faq/top.html
    c.l.c++ FAQ: http://www.parashift.com/c -faq-lite/
     
    Bart van Ingen Schenau, May 14, 2007
    #4
  5. Michael Frederick

    Roger Walker Guest

    Thanks for the replies, never even thought about moving it to a char
    array. Don't even ask why I was thinking of masks when it should have
    been mods!
     
    Roger Walker, May 14, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Schnoffos
    Replies:
    2
    Views:
    1,227
    Martien Verbruggen
    Jun 27, 2003
  2. Hal Styli
    Replies:
    14
    Views:
    1,657
    Old Wolf
    Jan 20, 2004
  3. Replies:
    10
    Views:
    783
    Charles Richmond
    May 21, 2005
  4. arun
    Replies:
    8
    Views:
    460
    Dave Thompson
    Jul 31, 2006
  5. aling
    Replies:
    8
    Views:
    961
    Jim Langston
    Oct 20, 2005
Loading...

Share This Page