Yahoo Groups archive

AVR-Chat

Index last updated: 2026-04-28 22:41 UTC

Thread

some C string conversion functions?

some C string conversion functions?

2005-04-22 by arhodes19044

I found itoa() in the stdlib.h.

I see the modulo function for floats, but is there an integer version 
of modulo?  I did not see that.

Is there a "dtoa" to convert a float to ascii?  I seem to remember 
something from my days when I used C very regularly, but now that I am 
getting back into it, I can not recall.

-TOny

Re: some C string conversion functions?

2005-04-22 by arhodes19044

Oh, yeah!!!  %  I forgot about that operator!  Thanks!  Yes it was 
not really a sting issue, but I needed to know that one too.  

-Tony

--- In AVR-Chat@yahoogroups.com, Colin Paul Gloster 
<Colin_Paul_Gloster@A...> wrote:
> On Fri, Apr 22, 2005 at 02:18:15PM -0000, Tony wrote:
> 
> "[..]
> 
> I see the modulo function for floats, but is there an integer 
version 
> of modulo?  I did not see that."
> 
> This has nothing to do with strings. C does not need an integral 
modulo
> function because it provides the % modulo operator for this as 
part of the
> language itself.
> 
> 
> "I found itoa() in the stdlib.h.
> 
> [..]
> 
> Is there a "dtoa" to convert a float to ascii?  I seem to remember 
> something from my days when I used C very regularly, but now that 
I am 
> getting back into it, I can not recall."
> 
> Third parties have written such functions but ANSI had itoa() and 
not its
> converse.

Re: some C string conversion functions?

2005-04-22 by arhodes19044

I have not used the printf functions yet since I just started using 
these uC's.  But, don't I have to use printf and its variants with a 
certain output stream?

I have an LCD display setup as a 4-bit parallel device.  I do not have 
it set up for any interrupt driven output, but I might do that to 
increase speed in the calling program.  Right now I just send the 
stuff character bycharacter until it is done and then come back to the 
calling routine.  I will have to see if sprintf will work in such a 
way.

Probably this is telling me to write an interrupt driven LCD stream!  
I have not seen such a beast pre-written.

-Tony

--- In AVR-Chat@yahoogroups.com, Brian Dean <bsd@b...> wrote:
Show quoted textHide quoted text
> On Fri, Apr 22, 2005 at 02:18:15PM -0000, arhodes19044 wrote:
> 
> > Is there a "dtoa" to convert a float to ascii?  I seem to remember
> > something from my days when I used C very regularly, but now that I
> > am getting back into it, I can not recall.
> 
> 'sprintf()' works.
> 
> -Brian
> -- 
> Brian Dean
> BDMICRO - ATmega128 Based MAVRIC Controllers
> http://www.bdmicro.com/

Re: [AVR-Chat] some C string conversion functions?

2005-04-22 by Colin Paul Gloster

On Fri, Apr 22, 2005 at 02:18:15PM -0000, Tony wrote:

"[..]

I see the modulo function for floats, but is there an integer version 
of modulo?  I did not see that."

This has nothing to do with strings. C does not need an integral modulo
function because it provides the % modulo operator for this as part of the
language itself.


"I found itoa() in the stdlib.h.

[..]

Is there a "dtoa" to convert a float to ascii?  I seem to remember 
something from my days when I used C very regularly, but now that I am 
getting back into it, I can not recall."

Third parties have written such functions but ANSI had itoa() and not its
converse.

Re: [AVR-Chat] some C string conversion functions?

2005-04-22 by Robert Adsett

At 02:18 PM 4/22/05 +0000, arhodes19044 wrote:
>I see the modulo function for floats, but is there an integer version
>of modulo?  I did not see that.

%

Robert

" 'Freedom' has no meaning of itself.  There are always restrictions,   be 
they legal, genetic, or physical.  If you don't believe me, try to chew a 
radio signal. "  -- Kelvin Throop, III
http://www.aeolusdevelopment.com/

Re: [AVR-Chat] some C string conversion functions?

2005-04-22 by Brian Dean

On Fri, Apr 22, 2005 at 02:18:15PM -0000, arhodes19044 wrote:

> Is there a "dtoa" to convert a float to ascii?  I seem to remember
> something from my days when I used C very regularly, but now that I
> am getting back into it, I can not recall.

'sprintf()' works.

-Brian
-- 
Brian Dean
BDMICRO - ATmega128 Based MAVRIC Controllers
http://www.bdmicro.com/

Re: [AVR-Chat] Re: some C string conversion functions?

2005-04-22 by Brian Dean

On Fri, Apr 22, 2005 at 03:05:51PM -0000, arhodes19044 wrote:

> I have not used the printf functions yet since I just started using
> these uC's.  But, don't I have to use printf and its variants with a
> certain output stream?

Not with 'sprintf()' which writes its output into a memory buffer.  A
simple way to convert a float to ascii is simply to:

       #include <avr/pgmspace.h>
       #include <stdio.h>

       ...

       sprintf_P(my_buf, PSTR("%f"), my_float);

       ...

Where 'my_buf' is a char [] big enough to hold the result.

If you use 'printf()', then yes, you need to provide some type of
output function, but it is quite flexible.  Initialize like this:

int my_putc(char ch)
{
  ...

  return ch;
}

  ...

  /* initialize libc stdio so 'printf()' works */
  fdevopen(my_putc, NULL, 0);

  printf_P(PSTR("Hello world!\n"));

  ...


The 'my_putc()' function just needs to be able to handle output a
character at a time, but other than that, it can drive any output
device that you can dream up.  For simple polled serial, it's very
simple, something like:

int my_putc(char ch)
{
  loop_until_bit_is_set(UCSR0A, UDRE);
  UDR0 = ch;
  return ch;
}

Of course, you will need to initialize the UART also, before calling
'printf()'.

For interrupt driven serial, it's slightly more complicated -
basically put the character into a buffer and let interrupt driven I/O
continue the process.  That way, your 'printf()' can return
immediately if that is important.

> I have an LCD display setup as a 4-bit parallel device.  I do not have
> it set up for any interrupt driven output, but I might do that to
> increase speed in the calling program.  Right now I just send the
> stuff character bycharacter until it is done and then come back to the
> calling routine.  I will have to see if sprintf will work in such a
> way.

'sprintf()' will not work that way, but you can easily make 'printf()'
work as described above by simply providing your LCD equivalent of
'my_putc()' above.

For more information on this, see the avr-libc documentation here:

     http://www.nongnu.org/avr-libc/user-manual/

Specifically:

     http://www.nongnu.org/avr-libc/user-manual/group__avr__stdio.html

-Brian
-- 
Brian Dean
BDMICRO - ATmega128 Based MAVRIC Controllers
http://www.bdmicro.com/

Move to quarantaine

This moves the raw source file on disk only. The archive index is not changed automatically, so you still need to run a manual refresh afterward.