Continue to Site

# split a number by 3

Status
Not open for further replies.

#### AtomSoft

##### Well-Known Member
Hey guys i got stuck here heh

I have a number like 255 and want to split it to

2,5,5

how?

Im trying this:

tH = (temp4 / 100) + 0x30;
tT = ((temp4 % 100) / 10) + 0x30;
tO = (((temp4 % 100) / 10) % 10) + 0x30;

If temp4 = 004 aka 0x04 my variables are

tH = 0;
tT = 0;
tO = 0;

any thoughts on why?

#### AtomSoft

##### Well-Known Member
I think i got it :

Code:
        tH = (temp4 / 100) + 0x30;
tT = ((temp4 % 100) / 10) + 0x30;
tO = (((temp4 % 100) % 10) % 10) + 0x30;

new topic...

#### Attachments

• temper.jpg
41.4 KB · Views: 728
• quote.jpg
3.5 KB · Views: 597
Last edited:

Temperature Data

Post deleted

Last edited:

New topic...

Last edited:

#### Mr RB

##### Well-Known Member
This compiles MUCH smaller (and needs no compiler math libraries linked in), and runs much faster;

Code:
tH = 0;
while(temp4 >= 100)
{
tH++;
temp4 -= 100;
}
tT = 0;
while(temp4 >= 10)
{
tT++;
temp4 -= 10;
}
tO = temp4;
// (note temp4 is destroyed during the process)

#### Noggin

##### Member
While not nearly as efficient, this is very simple

Code:
char buffer[4];
sprintf(buffer, "%u", temp4);

buffer is now '2''5''5''\0'

#### UTMonkey

##### New Member
I would go for Noggins suggestion or something like ITOA()

#### AtomSoft

##### Well-Known Member
i never understood sprintf.. can someone link me some info on it?

#### Noggin

##### Member
i never understood sprintf.. can someone link me some info on it?

In general:

sprintf( (char*)destination, text string, variable, variable, variable, ... )

char buffer[100]

int nbr_of_apples = 17
float gallons_of_water 2.487
char name[] = "John Doe"

sprintf( buffer, "I have %u applies", nbr_of_apples")
buffer = I have 17 apples

sprintf( buffer, "I have %u apples floating in %f gallons of water", nbr_of_apples, gallons_of_water)
buffer = I have 17 apples floating in 2.48700000 gallons of water (not sure how many 0's would actually be there)

sprintf( buffer, "My name is %s.", name )
buffer = My name is John Doe.

Hopefully that makes sense, you have a destination buffer, a format string, and variables. For every %, you need a u (unsigned intiger) d (signed integer) f (float) x (ascii representation of a unsigned int lowercase) or X (ascii representation of an unsigned int uppercase) s (string). If you want a % printed to the buffer, use %%

I usually go to Wikipedia and search for string.h to get this info if I forget anything. Once you use it, it becomes REALLY simple to format numbers into strings. However, it is fairly slow to execute (I think) and is a pretty large memory hog.

#### colin mac

##### New Member
Code:
int num = 255;
int a,b,c;

a = (num / 100 ) % 10;    //a = 2;
b = ( num / 10) % 10;    //b = 5;
c = num % 10;           // c = 5;

#### colin mac

##### New Member
I would go for Noggins suggestion or something like ITOA()

itoa() is a non-standard function. Some compiler implement it, some don't. It's best to avoid it.

#### UTMonkey

##### New Member
Granted - its about a portable as the C language for the PIC

#### Oznog

##### Active Member
1. Sprintf cannot check for how big the buffer is for the pointer which you passed to it. If you make it print out something too long, it will happily overrun the buffer and write to space not reserved for that char buffer. Could be space other variables were placed in- and your code will malfunction in weird and difficult-to-debug ways.
2. sprintf, like all strings, always terminates the string will a NULL char. That's how string ops know where the char pointer provided is done with chars, regardless of the length it was created in. It also means that the buffer needs to be at least one char larger than the string you print out. Note that CR and LF do not terminate a string as far as string ops are concerned, NULL does.

#### Noggin

##### Member
3. Sprintf doesn't warn you if you have too few or too many variables than are specified by your format string

#### AllVol

##### New Member
Code:
int num = 255;
int a,b,c;

a = (num / 100 ) % 10;    //a = 2;
b = ( num / 10) % 10;    //b = 5;
c = num % 10;           // c = 5;

This is the way I've found to accomplish the task. Works well for me.

#### AtomSoft

##### Well-Known Member
heh ok ok i can split the number fine now heh

i used something like:

Code:
int num = 255;
int a,b,c;

a = (num / 100 ) % 10;    //a = 2;
b = ( num / 10) % 10;    //b = 5;
c = num % 10;           // c = 5;

#### Mr RB

##### Well-Known Member
Well it's still heaps slower than the way I posted, but if you want to use it you can improve it a bit by changing;

Code:
a = (num / 100 ) % 10;    //a = 2;

to;

Code:
a = (num / 100 );    //a = 2;

Status
Not open for further replies.

Replies
56
Views
7K
Replies
4
Views
2K
Replies
8
Views
2K
Replies
7
Views
4K
Replies
11
Views
881