1 Reply Latest reply on Apr 2, 2018 2:21 AM by anks

    Most efficient way to get integer from buffer?

    jeremy_3270776

      Hello, everyone!

       

      I'm trying to get information from the buffer, which is brought in as a char array, and convert it to an integer.  I'm trying to stop reading the value when it reaches a comma.

       

      For example, if I'm getting the following:

       

      3,

       

      I'm hoping to read the 3 as "3" rather than the ascii value "51".

       

      Likewise, if I get the following:

       

      33,

       

      I'd like to be able to read 33.

       

      Currently, I'm coding it as shown below:

       

           uchar8 buffersize = 0;

       

           while(buffer[i]!=',')

           {

                buffersize = buffersize*10+atoi(buffer[i]);

                i++;

           }

       

      Unfortunately, I can't get the atoi to work correctly (maybe I'm using it wrong and it needs to be some other format going in?)

       

      The alternative is obviously something like this:

       

           uchar8 buffersize = 0;

       

           while(buffer[i]!=',')

           {

                buffersize = buffersize*10+(buffer[i]-48);

                i++;

           }

       

      It's not particularly efficient but it gets the job done.  This did leave me wondering if there was a better way to handle this (or what I need to add to get atoi up and working.  I have #include <stdio.h> included in my project but maybe it's not in that library (apparently, it's not or I assume it would be working . . . heh).

       

      Anyway, I do have it working but if anyone else has any recommendations as to the best way to convert a character array into an integer (and how to parse it, either before or after), please let me know.  Thanks a lot!