简体   繁体   中英

how to convert an integer to string?

I have a standard c function with the following prototype

extern void lcd_puts(const char *s);

in my other function i have something like this

send_to_lcd() {
  uint8_t count = 10
  lcd_puts(count);
}

my question is how do i convert count to a string pointer to be able to send it to lcd_puts which should print out the count on a lcd screen

thanks

On a microcontroller, you have to be at least a little worried about performance (which rules out sprintf ), even division is a very expensive operation on such chips. So you want code optimized for a microcontroller.

I've written some here: http://ideone.com/SsEUW (will need a few changes for use with C-style strings instead of C++, but the method should be clear)

It depends on what lcd_puts does with its argument. One possibility is as follows:

void send_to_lcd(uint8_t count)
{
    char str[SOME_CONSERVATIVE_MAX_LENGTH];
    sprintf(str, "%d", count);  // You might also snprintf() if it's available
    lcd_puts(str);
}

But remember that str goes out of scope as soon as send_to_lcd() returns. So if lcd_puts "remembers" its input argument, this will have undefined behaviour.

If that's the case, you will have to malloc a string buffer instead. But then you'll need to remember to free() it at some point, and it all gets rather messy.

This seems like a reasonable approach.

#include <stdint.h>
#include <stdio.h>

const char *u82s(uint8_t count)
{
    static char aString[4];

    aString[3] = '\0';
    aString[2] = (count % 10) + '0';  count /= 10;
    aString[1] = (count % 10) + '0';  count /= 10;
    aString[0] = (count % 10) + '0';

    return aString;
}

int main(void)
{
    uint8_t z = UINT8_MAX;

    do
    {
        z++;
        printf("%s\n", u8ts(z));
    }
    while (z != UINT8_MAX);

    return 0;
}

sprintf will format a string

Quick example:

char buf[50];
uint8_t count = 10;
sprintf(buf,'%d',count);

lcd_puts(buf);

Removed static and tidied up:

void UART_SendInt( uint16_t num )
{
    #define MAX_LEN         6   // 32767 is 6 characters with NULL terminator
    #define BASE_10         10  // Print decimal format

    uint8_t index = MAX_LEN - 1;
    char str[ MAX_LEN ];

    str[ index ] = '\0';
    while( index-- )
    {
        str[ index ] = ( num % BASE_10 ) + '0'; 
        num /= BASE_10;

        if( 0 == num )
        {
            UART_SendStr( &str[ index ] );
            break;
        }
    }

    UART_SendStr( "\r\n" );

    return;
}

I can't print above 32767 but i think that's something to do with my compiler options.

Taking your api as a basis.

/* One assumes this is a function that somehow displays a c string on the lcd. */
extern void lcd_puts(const char *s);


send_to_lcd() 
{
  uint8_t count = 10;     /* This is the variable to send to the screen         */ 

  lcd_puts(u82s(count));  /* This creates a string representation of count,     */
}                         /* which is then passed to the lcd_puts function      */
                          /* giving you the result you are after.  You question */
                          /* was how to make a c string out of a uint8.         */
                          /* this is a way to do it.                            */

It is basically the same as the answer you picked. Convert the value count into ac string, so that lcd_puts can use it.

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