This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

convert int to char to show in LCD



Hi I am using the IAR and I'm willing to show an int on a display, but the function only accepts char. I try to convert int to char with this function for example.

  int i = 10;
   char buffer [33];
   itoa (i, buffer, 10);
   PrintStr (buffer);


But it gives the following error: Error [e46]: Undefined external "itoa" referred in main

can someone explain what is happening?

  • itoa is not implemented in CCS or in IAR.

    Here's a link to various ANSI C implementations of the function

    http://en.wikibooks.org/wiki/C_Programming/C_Reference/stdlib.h/itoa

    Tony

  • i did this function to convert int to char, but it have a error, when i put float value like 24,543 he show in lcd, but when i put 33,000 for example, he don´t show anything in LCD.

    void main(){
     

     InitializeLcm();
     ClearLcmScreen();
     
      float valor = 33.000;
     
      int valora, valor0, valor1, valor2, valor3, valor4;  

      if (valor < 10){
        
    valora = valor * 1000;
    valor0 = valora / 1000;
    valor1 = valora / 100 - ( valor0 * 10);
    valor2 = valora / 10 - ( (( valor0 * 10) + valor1) * 10);
    valor3 = valora / 1 - ( (( valor0 * 100) + (valor1 *10) + valor2) * 10);

    imprime(valor0);
    PrintStr(".");
    imprime(valor1);
    imprime(valor2);
    imprime(valor3);
    PrintStr("V");

      }
     
      else if (valor > 10 ){
        
        valora = (float)valor * 1000;
        valor0 = valora / 10000;
        valor1 = valora / 1000 - ( valor0 * 10);
        valor2 = valora / 100 - ( (( valor0 * 10) + valor1) * 10);
        valor3 = valora / 10 - ( (( valor0 * 100) + (valor1 *10) + valor2) * 10);
        valor4 = valora / 1 - ( (( valor0 * 1000) + (valor1 *100) + (valor2 *10)) * 10);
        
          imprime(valor0);
          imprime(valor1);       
          PrintStr(".");      
          imprime(valor2);      
          imprime(valor3);      
          imprime(valor4);
          PrintStr("V");
      }

    }

    void imprime(int valor1){

    if(valor1 == 0){
    PrintStr("0");
    }
    else if(valor1 == 1){
    PrintStr("1");
    }
    else if(valor1 == 2){
    PrintStr("2");
    }
    else if(valor1 == 3){
    PrintStr("3");
    }
    else if(valor1 == 4){
    PrintStr("4");
    }
    else if(valor1 == 5){
    PrintStr("5");
    }
    else if(valor1 == 6){
    PrintStr("6");
    }
    else if(valor1 == 7){
    PrintStr("7");
    }
    else if(valor1 == 8){
    PrintStr("8");
    }
    else if(valor1 == 9){
    PrintStr("9");
    }

    }

    why this happened?

  • felipe paulo said:
      int valora, valor0, valor1, valor2, valor3, valor4;

    Think: what is the maximum value that can be stored in an int...?

  • I use to employ

    uint8, uint16...

    to avoid having this problems :P

  • realmente, troquei o int para long int, deu certo.

    obrigado

  • felipe paulo said:
    valora = (float)valor * 1000;
        valor0 = valora / 10000;


    Two cardinal sins.

    Using float is slooooooow. And significantly adds to the code size too,a s it includes a lot of library code. If you don't really, really, need floats, stay away from them.
    And second, divisions are slooow too. Float divisions are  (well, think of two lines of o's)

    If you use the search funciton in this board, you'll find an implementation that uses subtractions increments and comparisons (which the processor natively supports) for a fast conversion.

    And to answer your question about why it didn't work for 33.000: valora is an int and theiir maximum value is 32767. So your 33000 converts into a negative value, whcih you don't handle in your code.

  • Jens-Michael Gross said:
    Using float is slooooooow. And significantly adds to the code size too,a s it includes a lot of library code. If you don't really, really, need floats, stay away from them. And second, divisions are slooow too

    Indeed - if you have code space & execution time to burn, why not just use sprintf()...?!

  • sprintf don't work in IAR, itoa too. I change int to long int and worked

  • felipe paulo said:
    sprintf don't work in IAR

    I find that very hard to believe - sprintf is part of the standard 'C' library!

    felipe paulo said:
    itoa too

    The reason for that has already been explained.

  • Yes, i know. but when i put sprintf say

    Fatal Error[e89]: Too much object code produced (more than 0x1000 bytes) for this package

    then, don´t work.

  • That's because you've filled all your code space up with the floating point stuff!

  • Breathe, Andy, breathe...

  • felipe paulo said:
    but when i put sprintf say
    Fatal Error[e89]: Too much object code produced (more than 0x1000 bytes) for this package

    Well, using float divisions is not much smaller. Maybe just the few bytes that make a difference now, but as soon as you add some useful code to your project, you'll be hosed again.

**Attention** This is a public forum