Re: help needed on coversin of an char array to an integer
- From: Mark Bluemel <mark_bluemel@xxxxxxxxx>
- Date: Thu, 21 Feb 2008 14:07:19 +0000
MAx wrote:
Hi,
Im kinda stuck in a project at a point where i need an array to
be converted to a
integer using some kind of math.
This board does not support functions like scanf, sscanf etc as
it does not have enough memory to hold their stack.
given a string char str[8] = {'0', '0', '0' , '1' , 'f' , 'b' ,
'c' , 'a' };
i need a function which can convert this array to an integer
( 0x0001fbca )
Then write one. It's not exactly difficult.
* Start with a running total, set initially to 0.
* Get a digit at a time, convert it into a numeric value,
multiply the running total by 16 and add the digit's value.
* When you run out of digits, you're done
Converting a character (e.g. '9' or 'a') to it's numeric value is a
little more tricky, but not very.
According to the standard, '0' - '9' are contiguous in the character
set, therefore the can always be converted to integers by subtracting
'0', so that's easy.
If you can guarantee that your character set has contiguous alphabetics
(ASCII does, but some representations don't), you can covert alphabetic
characters to their value in the hexadecimal range by subtracting 'a'
(or 'A' as appropriate) and adding 10.
If your character set isn't contiguous (or you're not sure that it will
be so everywhere that your code may need to run), you'll need to use a
lookup table or switch block.
Expecting a quick response.
Nice to see an optimist.
.
- References:
- Prev by Date: Re: .h files
- Next by Date: Re: Writting Excel file
- Previous by thread: Re: help needed on coversin of an char array to an integer
- Next by thread: Re: help needed on coversin of an char array to an integer
- Index(es):
Relevant Pages
|