Unsigned int roll over at 32767 to a minus value

Go To Last Post
14 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The problem is i use value1 = value1 + 1;
And then convert value1 with itoa and display on a lcd all works ok except that
the displayed value roll over at 32767 and does not count to 65xxx .
i also use ' unsigned int value1'.what am i missing?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

itoa treats argument as signed integer, try utoa

David

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks i will try that im a newbie with c, i got a long back ground in pic's and assembler now playing with atmels.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Do note that utoa is expensive (in flash utilization) relative to itoa, and there have been several alternative methods of minimizing code size depending on just how the result might be used. I have used Peter Dannegger's C routine shown here, YMMV.

David

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Signrd/unsigned is often just a question of interpretation at the point of observation. For example if you make the mistake of using %d rather than %u on a U16 it will be shown with an I16 interpretation.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks again works ok now

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

frog_jr wrote:
Do note that utoa is expensive (in flash utilization) relative to itoa, ...

 

Indeed, there have been detailed discussions on binary=>BCD/ASCII over the years.  Much gory detail and evaluation of different approaches.

 

But now you've got me -- why would any utoa() implementation with the same approach as itoa() implementation be more expensive in flash words?  (If anything, less flash since the minus sign doesn't need to be handled.)

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

theusch wrote:

frog_jr wrote:
Do note that utoa is expensive (in flash utilization) relative to itoa, ...

 

Indeed, there have been detailed discussions on binary=>BCD/ASCII over the years.  Much gory detail and evaluation of different approaches.

 

But now you've got me -- why would any utoa() implementation with the same approach as itoa() implementation be more expensive in flash words?  (If anything, less flash since the minus sign doesn't need to be handled.)

 

Been wondering that myself... Perhaps because somebody wrote it using sprintf?

If you don't know my whole story, keep your mouth shut.

If you know my whole story, you're an accomplice. Keep your mouth shut. 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

utoa/itoa are just preamble to a common routine so I simply don't believe that the "cost" difference is any more than a handful of opcodes. 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I was just positive I had seen that utoa required more flash than itoa somewhere in the past, but had never verified it for myself.

So I just did a small test program and found itoa was actually 22 bytes longer (in that case) than utoa!

 

Oh well so much for relying on tibits of info floating around in the old grey matter...

David

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yup that handful of opcodes are about handling the sign and '-' so it makes sense that itoa would be the longer. They both effectively use the main body of utoa

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

frog_jr wrote:
So I just did a small test program and found itoa was actually 22 bytes longer (in that case) than utoa!

As I mentioned:

theusch wrote:
...any utoa() implementation with the same approach as itoa() implementation...

Surely y'all are pointing to a particular implementation.  GCC?  There are many approaches.  Some might indeed sacrifice code space for speed.

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Well, the source for the silly functions has to be SOMEWHERE.

If you don't know my whole story, keep your mouth shut.

If you know my whole story, you're an accomplice. Keep your mouth shut. 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

> I was just positive I had seen that utoa required more flash than itoa somewhere in the past

 

You've been fooled by some dumps.  The itoa implementation tail-calls utoa, hence any code using itoa will also use utoa (but not vice versa).

 

Moreover, the implementation you are referring to is limited to radix=10 wherea *toa is not.
 

avrfreaks does not support Opera. Profile inactive.