View previous topic :: View next topic |
Author |
Message |
Mattr0
Joined: 27 Mar 2005 Posts: 30
|
getting digits out of an int |
Posted: Sun Mar 27, 2005 2:45 pm |
|
|
How can I get the digits out of an interger
ex
if a = 255 then I want to, b = 2, c = 5, d = 5. In pic basic you could just do
b = a dig 0
is there a command like this in c or how would I do such a task |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Sun Mar 27, 2005 4:21 pm |
|
|
convert it to a string. look at sprintf or search the forum for more ideas. |
|
|
Guest
|
|
Posted: Mon Mar 28, 2005 10:24 am |
|
|
If you want to do it w/o a function, i found this handy little function in the program i'm working on. For my purposes, it only works on a number 99 or smaller, but you can build on it.
Code: |
// need a buff of size 2
// Preconditions: mybuff points to an int of less than 100.
void int_to_string_conv(unsigned int this_int, char *mybuff)
{
mybuff[0] = (this_int / 10) + 0x30;
mybuff[1] = (this_int % 10) + 0x30;
}
|
|
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Mon Mar 28, 2005 10:27 am |
|
|
Anonymous wrote: | If you want to do it w/o a function, i found this handy little function in the program i'm working on. For my purposes, it only works on a number 99 or smaller, but you can build on it.
Code: |
// need a buff of size 2
// Preconditions: mybuff points to an int of less than 100.
void int_to_string_conv(unsigned int this_int, char *mybuff)
{
mybuff[0] = (this_int / 10) + 0x30;
mybuff[1] = (this_int % 10) + 0x30;
}
|
|
Wouldn't this still be a function |
|
|
Guest
|
|
Posted: Tue Mar 29, 2005 12:49 pm |
|
|
Mark wrote: |
Wouldn't this still be a function |
oops, i meant a library function |
|
|
|