Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

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?
 
new topic...
 

Attachments

  • temper.jpg
    temper.jpg
    41.4 KB · Views: 801
  • quote.jpg
    quote.jpg
    3.5 KB · Views: 683
Last edited:
Temperature Data

Post deleted
 
Last edited:
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)
 
While not nearly as efficient, this is very simple

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

buffer is now '2''5''5''\0'
 
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.
 
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;
 
Two important notes about sprintf:
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.
 
3. Sprintf doesn't warn you if you have too few or too many variables than are specified by your format string
 
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.
 
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;
 
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.

Latest threads

New Articles From Microcontroller Tips

Back
Top