Velocity Reviews > Q: Get x digits from int

# 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?

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/

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

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/

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!