Convert usigned integer( uint16_t) to string. Standard itoa base 10 is giving negative values

user3278790 picture user3278790 · Jan 27, 2017 · Viewed 9.2k times · Source

I need to convert uint16_t value to a string. I want the string to be a decimal respresentation of the number.

Example: uint16_t i=256 string: 256

I tried with itoa(i,string, 10) but when i value increases starts printing negative values.
I send the string via the Serial Port.(UART)
It is there some alternative?

Answer

zmechanic picture zmechanic · Aug 23, 2017

Use sprintf with %u format for unsigned int:

uint16_t i = 33000;
sprintf(str, "%u", i);