Tell me more ×
Electrical Engineering Stack Exchange is a question and answer site for electronics and electrical engineering professionals, students, and enthusiasts. It's 100% free, no registration required.

I am trying to read data from the ADC and display it on a HD44870 compatible LCD with an ATmega32. As the data from the ADC is a 10-bit unsigned integer, the LCD expects a string, some conversion is necessary. Below is the function i wrote to accomplish this conversion.

char *int_to_str(uint16_t num, uint8_t len){
    uint8_t i;
    char *str;
    str[len]='\0';
    for(i=(len-1); i>=0; i--){
        str[i] = '0' + num % 10;
        num/=10;
    }
    return str;
}

However, the above function does not work. I just get a blank display where the numbers should be displayed. I am currently using itoa() and it works. However, i would prefer to write my own since size of the resulting executable is critical. Thank you.

share|improve this question
Check up on sprintf(). You are confusing integer and string manipulation. – jippie Jan 6 at 17:01
sprintf probably uses itoa internally,and is likely to be larger. – Brian Drummond Jan 6 at 18:16
Why do you think writing your own implementation of itoa() will be smaller? – Phil Frost Jan 7 at 3:39

1 Answer

There is no reason why sprintf wouldn't work. This basically prints a string to a buffer instead of standard output.

As an example

char string[256];
int value = 5;
memset(string,0,sizeof(string[0])*256); // Clear all to 0 so string properly represented
sprintf(string,"Value: %d",value);

string will now contain the string "Value: 5"

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.