Q: Get x digits from int

M

Michael Frederick

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

Boudewijn Dijkstra

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.
 
L

Lew Pitcher

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
 
B

Bart van Ingen Schenau

Boudewijn said:
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
 
R

Roger Walker

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!
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top