Re: Converting string to uint32_t

Andreas Höschler <[email protected]>
Newsgroups gmane.comp.hardware.avr.gcc
Message-ID <[email protected]>
Hi all,

thanks for your responses that helped a lot to solve the problem:

> The truncation is probably happening here--- utoa() takes an unsigned
> int, which is 16 bits on this architecture. Try using ultoa() instead
> and see if that helps. Compiling with the -Wconversion option turned on
> might warn about this kind of situation.


The working set of methods:

uint32_t getLong_from_serial_rec_buffer()
  {
   char char_buffer[10];
   int i;
   for (i = 0 ; i < 10 ; i++)
     {
      if (bytes_in_serial_rec_buffer() > 0)
        {
         char current = buffer[buffer_tail];
         if (current >= '0' && current <= '9')
           {
            get_from_serial_rec_buffer(); // we already know the byte :-)
            char_buffer[i] = current;
           }
         else // is a comma or NULL or whatever other character
           {
            char_buffer[i] = 0;
            uint32_t ret = (uint32_t)strtoul(char_buffer, NULL, 0);
            return ret;
           }
        }
     } 
   return -1;   
  }

void putLong_in_serial_trans_buffer(uint32_t value)
{
   char char_buffer[10];
   ltoa(value, char_buffer, 10); 
   int i;
   for (i = 0 ; i < 10 ; i++)
    {
      char current = char_buffer[i];
      if (current != 0) TxByte (current);
      else break;
    }
}

Thanks,

 Andreas

_______________________________________________
AVR-GCC-list mailing list
[email protected]
https://lists.nongnu.org/mailman/listinfo/avr-gcc-list
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.