View previous topic :: View next topic |
Author |
Message |
beg Guest
|
HELP!! how to convert CHAR to LONG INT |
Posted: Wed Apr 21, 2004 2:19 am |
|
|
new user's question,
how to convert "Char" data type into "Long Int" data type?
thanks in advance!! |
|
|
Ttelmah Guest
|
Re: HELP!! how to convert CHAR to LONG INT |
Posted: Wed Apr 21, 2004 2:38 am |
|
|
beg wrote: | new user's question,
how to convert "Char" data type into "Long Int" data type?
thanks in advance!! |
Cast.
Conversions like this are 'semi automatic'. You can just use:
long int fred;
char chr;
fred=chr;
However if you want to force arithmetic to use the 'long int' equivalent, rather than being done using the char type, and then converted at the end, use the C 'cast', like:
fred=(long int)chr * 10;
Which forces 'chr' to be converted into a long int, before being multiplied by 10 in this example.
Best Wishes |
|
|
DragonPIC
Joined: 11 Nov 2003 Posts: 118
|
atoi() |
Posted: Wed Apr 21, 2004 11:38 am |
|
|
unless you are converting a ASCII # to a number. Then you would use atoi() or , if not a string of characters, mask off the high nibble of char byte.
Code: | char chr = '9'; //hex 0x39
long num;
num = chr & 0x0F;
//if hex
num = chr;
if (num > 0x39)
{
num &= 0x0F;
num += 0x09;
}
else
num &= 0x0F; |
I'm sure you can find other more simple ways to do it. |
|
|
|