Adding #include <extras.h> into CW project so I can use ltoa function

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Adding #include <extras.h> into CW project so I can use ltoa function

1,509 Views
steve_fae
Senior Contributor I

Hack programmer here.  Need to convert 32 bit integer to string.  Found ltoa function in MSL reference and it looks like it will perform this perfectly.

I can't seem to add the extras.h to the project include path to make the compiler happy.  It keeps complaining that there is no such directory or file.

Here is the exact path C:\Freescale\CW MCU v10.3\MCU\ARM_GCC_Support\ewl\EWL_C\include\extras

Labels (1)
0 Kudos
7 Replies

841 Views
LuisCasado
NXP Employee
NXP Employee

Hi Steven,

Add #include "stdlib.h" is enough.

This code works with a new project wizard for KL25:


#include "derivative.h" /* include peripheral declarations */

#include "stdlib.h"

 

int main(void)

{

               int counter = 0;

              

              

               counter= atoi("1234");

              

               for(;;) {    

                              counter++;

               }

              

               return 0;

}

0 Kudos

841 Views
JimDon
Senior Contributor III

I have to agree with Luis.

I have never used a "C" compiler whose libs failed to provide this, and including stdlib.h should provide the prototype.

It is part of the cstdlib.

atoi - C++ Reference

Also, in a project I looked at:

"${MCUToolsBaseDir}/ARM_GCC_Support/ewl/EWL_C/include" this is where stdlib.h is found.

Was already in the gcc include paths with no issue, using the arm-none-eabi-gcc.

I most cases, I never recommend re-writing well tested lib functions, but perhaps in academia this is ok.

HOWEVER the function the op needs is ltoa, so how I would solve this myself is to use sprintf, if you have enough flash or are already using printf (as most of the code is already linked in).

   char buffer[16];

   sprintf(buffer,"%d",1234L);

0 Kudos

841 Views
steve_fae
Senior Contributor I

I wasn't very clear.  Erich posted a great example of a CDC project which basically takes the input buffer which is an ascii string then returns the same ascii string with a string message added in front of it.

I am trying to hack his project and instead send an ascii string with an ascii integer representation of a count value.

something like the itoa function but I can't seem to make the compiler happy with the include file.

int temp_log;

temp_log = 0xffff;

then use the below function to convert to string and return it

itoa()

This function converts an int value to a null-terminated character array.

#include <extras/extras_stdlib.h> 

char * itoa(int val, char *str, int radix); 


s

0 Kudos

841 Views
JimDon
Senior Contributor III

Right.

This takes an int an converts it to a null terminated character array:

char buffer[16];

   sprintf(buffer,"%d",myint);

This note concerning itoa():

" This function is not defined in ANSI-C and is not part of C++, but is supported by some compilers.

A standard-compliant alternative for some cases may be sprintf

:

  • sprintf(str,"%d",value) converts to decimal base.
  • sprintf(str,"%x",value) converts to hexadecimal base.
  • sprintf(str,"%o",value) converts to octal base.

See:

itoa - C++ Reference

So if you which to fight the system an get itoa to work, ok, but I wouldn't.

0 Kudos

841 Views
steve_fae
Senior Contributor I

thanks, lemme give it a try

0 Kudos

841 Views
JimDon
Senior Contributor III

Or use Erich's

UTIL1_strcatNum32u(buf, sizeof(buf), val);

(See his post on the other thread, I think he meant that to be here).


0 Kudos

841 Views
BlackNight
NXP Employee
NXP Employee

You could add

"${MCUToolsBaseDir}/ARM_GCC_Support/ewl/EWL_Runtime/include"

to your ARM gcc compiler include path directories (project options).

I did this, but then I got tons of other errors. It looks like that part of the EWL libraries is not adopted for gcc.

Anyway, atoi() is not part of the ANSI standard libraries, so using/depending on it will not be portable. That's why I ended up to have my own version of it. For this, I have added it to my Utility Processor Expert component (based on an open source version of ChaN (yes, the same famous person who wrote that FatFs :smileyhappy:). Below is the source:

/*------------------------------------------------------------------------/

/  Universal string handler for user console interface

/-------------------------------------------------------------------------/

/

/  Copyright (C) 2010, ChaN, all right reserved.

/

/ * This software is a free software and there is NO WARRANTY.

/ * No restriction on use. You can use, modify and redistribute it for

/   personal, non-profit or commercial products UNDER YOUR RESPONSIBILITY.

/ * Redistributions of source code must retain the above copyright notice.

/

/-------------------------------------------------------------------------*/

byte UTIL1_xatoi(const unsigned char **str, long *res)

{

/* 123 -5   0x3ff 0b1111 0377 3.25  w "

      ^                               1st call returns 123 and next ptr

         ^                            2nd call returns -5 and next ptr

                 ^                    3rd call returns 1023 and next ptr

                        ^             4th call returns 15 and next ptr

                             ^        5th call returns 255 and next ptr

                               ^      6th call returns 3 and next ptr, caller needs to read '.'

                                 ^    7th call returns 25 and next ptr

                                    ^ 8th call fails and returns ERR_FAILED

*/

  unsigned long val;

  unsigned char c, r, s = 0;

  *res = 0;

  while (**str==' ') {

    (*str)++;                          /* Skip leading spaces */

  }

  c = **str;

  if (c == '-') {                      /* negative? */

    s = 1;

    c = *(++(*str));

  }

  if (c == '0') {

    c = *(++(*str));

    switch (c) {

      case 'x':                        /* hexadecimal */

        r = 16; c = *(++(*str));

        break;

      case 'b':                        /* binary */

        r = 2; c = *(++(*str));

        break;

      default:

        if (c <= ' ' || c == '.') {

          return ERR_OK;               /* single zero */

        }

        if (c < '0' || c > '9') {

          return ERR_FAILED;           /* invalid char */

        }

        r = 8;                         /* octal */

        break;

    } /* switch */

  } else {

    if (c < '0' || c > '9') {

      return ERR_FAILED;               /* EOL or invalid char */

    }

    r = 10;                            /* decimal */

  }

  val = 0;

  while (c > ' ' && c != '.') {

    if (c >= 'a') c -= 0x20;

    c -= '0';

    if (c >= 17) {

      c -= 7;

      if (c <= 9) return ERR_FAILED;   /* invalid char */

    }

    if (c >= r) return ERR_FAILED;     /* invalid char for current radix */

    val = val * r + c;

    c = *(++(*str));

  } /* while */

  if (s) val = 0 - val;                /* apply sign if needed */

  *res = (long)val;

  return ERR_OK;

}


You can use zero for ERR_OK, and non-zero for ERR_FAILED. Or use the Utility component. The Utility module contains other functions to scan or convert numbers, just in case. The sources for it are on GitHub.

0 Kudos