简体   繁体   中英

How to convert integer to string to print integer on UART

I want to print out an integer value which is computed in the program on a uart screen. I am basically creating a data logging system. The user must be able to view the times when a character arrives.

I am not able to use printf or snprintf or sprint as i am creating a time triggered system and printf takes some time to execute. I have searched stackoverflow and found a few idea, however they do not seem to work. eg itoa, char c int i = 26; c = (char) i ( does not work but gives weird characters)

Thank you

In your example you try to print 26 decimal. This is not a printable character in the ASCII character set. You would have to convert the 2 to ASCII (50 decimal) and then the 6 to ASCII (Decimal 54) to print 26. In other words it would have to be one digit at a time.

You could do this by iteratively adding decimal 48 to each digit in your variable (the offset for ASCII zero) and load the ASCII digit from most significant digit to least in a buffer and transmit that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM