So in the midst of optimising and writing some conversion functions I have discovered an issue - its most likely because I have been babied by 'C' compilers in the past but perhaps I am missing something... (don't ask the misses, she would agree... )
So because I want a smallest foot print in code as I can get I have *arbitrarily* limited myself to <stdio.h> one of the functions I need is an ascii to int function -
here is a simple bit of C code to do just that
Code: Select all
int ascii_to_int16( char *str ) // this is limited to +65535 ( or -32758 / +32767 unsigned)
{
int i=0;
int num = 0;
int number_of_digits = 0;
int magnitude = 1; // (starting at the end of the string)
// every left shift in the string we multiply by 10
while( str[ i ] != '\0') // not including string.h - keep as much stdio as possible
{ // strlen would be nice but a quick dirty way to save a few
i++; // Kbytes by not including (string.h)
}
number_of_digits = i; // this includes the null byte terminator
for (i = (number_of_digits - 1); i >= 0; i--) // read from the far right of the string to the left
{
num += (str [i] - CHAR_TO_INT) * magnitude; // -48 to the ascii value to derive the int value
magnitude *= 10; // the int value is multiplied by the magnitude
} // is incresed (*10)
return num;
}
I have tried using "long" prefix with int, but sadly I couldn't display a 32 bit word ... saying that it may of been the display routine... but I thought I would check to see if long words (32bit integers) are supported
Thanks for reading