Converting a [char] variable to [int] then back to [char]

659 views Asked by At

I am using Visual C 6

I am trying to convert a character array (single-quotation) into an integer, then incrementing the value by 1, then storing the result back into a different character array..

But I keep getting an unexpected value when converting back to character..

Here is my code

   char char_array[4];
   char_array[0] = '1';
   char_array[1] = '2';
   char_array[2] = '3';
   char_array[3] = '\0';  //Terminating character

   int my_number = atoi(char_array);  
   printf("my_number = %d" , my_number);   // output is 123

   my_number++;    // works and my_number is incremented =124
   printf("now: my_number = %d" , my_number);   // output is 124


   char result[4];  //declared to store the result

   result = itoa(my_number);  // Output is unexpected.

   printf("%c", result[0]);    // Output is  2  instead of 1
   printf("%c", result[1]);    // Output is  2
   printf("%c", result[2]);    // Output as  3   instead of 4

It seems that the function itoa() somehow knows the original value 123 and in some weird way knows that I have incremented that value.. but the addition is done to the wrong digit. Instead of adding 1 to the least significant digit, the addition is done to the most significant digit.

4

There are 4 answers

2
Bill Lynch On BEST ANSWER

I find it really difficult to believe that your compiler is letting this code through:

char result[4];  //declared to store the result
result = itoa(my_number);  // Output is unexpected.

For one reason, you're attempting to reseat an array. Which shouldn't be allowed. For another, itoa() normally takes three arguments. It's prototype should look like:

char *itoa(int value, char * str, int base);

So you should be calling it as:

char result[4];
itoa(my_number, result, 10);

Or, if you'd like to use portable functions that don't have possible buffer overflows:

char result[4];
snprintf(result, 4, "%d", my_number);
2
Gyapti Jain On

itoa is not a standard C library function.

You can use

char result[sizeof(int) * CHAR_BIT / 10 * 3 + 4];  // '-1', '\0', max sizeof int on my 4 byte machine
// 10 bits are roughly equal to 3 digits at decimal base, extra 4 for '-', '\0', extra digit and safe character
sprintf(result, "%d", my_number);

If you still want to use itoa, consult the documentation of this function (in library/compiler documentation)

0
Gopi On

my_number is incremented and hence if you are using itoa() then it will know the new value of my_number which is 124.

Check the code below:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
   char char_array[4];
   char_array[0] = '1';
   char_array[1] = '2';
   char_array[2] = '3';
   char_array[3] = '\0';  //Terminating character

   int my_number = atoi(char_array);  
   printf("my_number = %d" , my_number);   // output is 123

   my_number++;    // works and my_number is incremented =124
   printf("now: my_number = %d" , my_number);   // output is 124


   char result[4];  //declared to store the result

   snprintf(result,4,"%d",my_number);
   printf("%c", result[0]);    
   printf("%c", result[1]);    
   printf("%c", result[2]);    
   return 0;
}
0
yuan su On

First the itoa(my_number) maybe wrong, I only know the followed function:

char *  itoa ( int value, char * str, int base );

str should be an array long enough to contain any possible value: (sizeof(int)*8+1) for radix=2 i.e. 17 bytes in 16-bits platforms and 33 in 32-bits platforms.