avr-gcc-list
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [avr-gcc-list] Converting string to uint32_t


From: Andreas Höschler
Subject: Re: [avr-gcc-list] Converting string to uint32_t
Date: Tue, 14 Oct 2014 19:30:59 +0200

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


reply via email to

[Prev in Thread] Current Thread [Next in Thread]