0

I have the following program in which I need to convert my_string to an integer because it has some alphabets and numbers. I get the integer value in value_i variable. I have checked that with if statement but now I need to be sure that I have extracted the integer value correctly. The function "itoa" does not work if I enable the statement.

 int value_i;
 char value_s[4]; 
 value_i = atoi(my_string); // This statement is working 
 // itoa(value_i, value_s, 4); // I get error when I enable the statement.  
 EUSART_Write("\n\rThe value is: \n\r");
 EUSART_Write(value_s);
engr_john
  • 27
  • 2
  • 4
    You did not specify what error did you get, also provide more code, i don't see a declaration for variable "my_string" even though your question mentions it. Provide output you expected and the output you got. – msaw328 Aug 26 '21 at 08:29
  • 4
    Show the content of `my_string`. Avoid using `atoi()` it has no error reporting and will happily accept `atoi ("my cow");` silently returning zero without any indication of error. Use `strtol()` (or at least `sscanf()`) which can provide indication of whether the conversion succeeds or fails. See [atol() v/s. strtol()](https://stackoverflow.com/q/3792663/3422102) – David C. Rankin Aug 26 '21 at 08:31
  • 1
    The third parameter in itoa is the base you want it in, typically. 2,8,10 or 16. Check https://www.cplusplus.com/reference/cstdlib/itoa/ – rmfeldt Aug 26 '21 at 08:32
  • @rmfeldt [C] and [C++] are different languages. The reference you cite `itoa()` isn't the same as `atoi()` (in fact it isn't part of the C-standard at all -- though you can find some good implementations on this site) – David C. Rankin Aug 26 '21 at 08:35
  • engr_john, what hardware are you using and what compiler. Your questions shows `itoa(value_i, value_s, 4);` gives the error (`itoa()` isn't part of standard C) So what compiler, etc.. Please provide [A Minimal, Complete, and Verifiable Example (MCVE)](http://stackoverflow.com/help/mcve). – David C. Rankin Aug 26 '21 at 08:38
  • 2
    @DavidC.Rankin: The function `itoa` isn't part of ISO C++, either. – Andreas Wenzel Aug 26 '21 at 08:38
  • @DavidC.Rankin They are different languages but the C standard lib is part of C++ via inclusion into the C++ standard. `itoa()` is neither in the C nor in the C++ standard (as the web page correctly states). – Peter - Reinstate Monica Aug 26 '21 at 08:39
  • 1
    The standard C library way to convert it is to use `snprintf` (or alternatively, `sprintf` when there is no need to specify the destination buffer size), declared by `#include `. For example: `snprintf(value_s, sizeof(value_s), "%d", value_i);`. – Ian Abbott Aug 26 '21 at 08:40
  • @IanAbbott And, I would argue, it is also the C++ standard way ;-). – Peter - Reinstate Monica Aug 26 '21 at 08:40
  • 1
    The string `my_string` declared as `char* my_string[10];` and it contains 205A at some point in the program. The variable `value_i` get 205 and I have tested it in the if statement but I am not able to convert to string to send echo back as string. – engr_john Aug 26 '21 at 08:41
  • 2
    @engr_john Do not bury that in a comment -- edit your question (both the declaration and the example input)! Preferably, create a minimal program that compiles and runs. – Peter - Reinstate Monica Aug 26 '21 at 08:42
  • @IanAbbott , you can use `int chars = snprintf (NULL, 0, "%d", value_i):` to determine the number of characters needed. Then allocate for `my_string` (`chars` +1 for the `'\0'`) and use `sprintf (my_string, "%d", valie_i);` to complete the conversion. (Or simply use `char my_string[32];` which is more than sufficient for all integer values in base 10 through `int64_t`) – David C. Rankin Aug 26 '21 at 08:46

1 Answers1

0

// itoa(value_i, value_s, 4); // I get error when I enable the statement.

itoa() is not a standard C library function.

If it exist, (there are many forms as it is not standard), char value_s[4]; may simple be too small.

Depending in the error, using a larger size may work: Possible solution.

char value_s[12];  // Big enough for -2147483648
itoa(value_i, value_s, sizeof value_s);

Otherwise use standard snprintf().

char value_s[12];  // Big enough for -2147483648
int len = snprintf(value_s, sizeof value_s, "%d", value_i);
if (len < 0 || len >= sizeof value_s) Handle_rare_error();

OP later comments:

The string my_string declared as char* my_string[10]; and it contains 205A at some point in the program. The variable value_i get 205 and I have tested it in the if statement but I am not able to convert to string to send echo back as string.

If true, value_i = atoi(my_string); is bad code.

Save time: Enable all compiler warnings.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256