Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Q: Get x digits from int

Reply
Thread Tools

Q: Get x digits from int

 
 
Michael Frederick
Guest
Posts: n/a
 
      05-14-2007
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?
 
Reply With Quote
 
 
 
 
Boudewijn Dijkstra
Guest
Posts: n/a
 
      05-14-2007
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/
 
Reply With Quote
 
 
 
 
Lew Pitcher
Guest
Posts: n/a
 
      05-14-2007
On May 14, 8:12 am, Michael Frederick <n...@email.address> 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


 
Reply With Quote
 
Bart van Ingen Schenau
Guest
Posts: n/a
 
      05-14-2007
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/
 
Reply With Quote
 
Roger Walker
Guest
Posts: n/a
 
      05-14-2007
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!
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Difference between int i, j; and int i; int j; arun C Programming 8 07-31-2006 05:11 AM
int a[10]; int* p=(int*)((&a)+1); But why p isn't equal to ((&a)+1)? aling C++ 8 10-20-2005 02:42 PM
How to get number of digits in int variable? guidosh@gmail.com C Programming 10 05-21-2005 11:12 AM
int main(int argc, char *argv[] ) vs int main(int argc, char **argv ) Hal Styli C Programming 14 01-20-2004 10:00 PM
dirty stuff: f(int,int) cast to f(struct{int,int}) Schnoffos C Programming 2 06-27-2003 03:13 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57