Re: help needed on coversin of an char array to an integer



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.
.



Relevant Pages

  • Re: string generation
    ... >>> I want to generate every possible 16 character combination of the ... Yes, but it still sounds like a cracking device, Dangermouse! ... constructing the actual array is, er, infeasible, that hardly matters. ... converting to base 62, but neither is difficult.) ...
    (comp.lang.c)
  • RE: Converting a string to an array?
    ... Converting a string to an array? ... string into a character array. ...
    (comp.lang.python)
  • Re: My code to determine whether two words are anagrams wont work.
    ... traversing them and storing the character counts in an array. ... is an anagram a meaningful concept in chinese? ... but they aren't related to character set. ...
    (comp.lang.c)
  • Re: A note on computing thugs and coding bums
    ... code is valid for any character set that is legal in C (which is a ... characters in the required source character set ... A String, in C Sharp or Java, can be redefined. ... allow programmers to handle some other data format, ...
    (comp.programming)
  • Re: [QUIZ] Bytecode Compiler (#100)
    ... So having written the lexer class, I now set up the state transition ... The tokens are ... # The lexer needs to know the character sets involved in deciding ... # Initialize the character set columns to be used by the lexer. ...
    (comp.lang.ruby)