You are here:

C/converting from integer to ascii??

Advertisement


Question
Hi,

Thanks for the code, but I enter in let's say, 123 as integer. And then when I run program, it tells me that I've entered '321'. So that actually means that '321', which is in ascii code represents 123?

Also, I was just trying to use putchar instead of printf to print out the results. I thought that wouldn't be too hard to do, which is why I got into converting integer to ascii idea but still. I know you mentioned about negative numbers in your last reply, but I'm not concerned about negative numbers at this point.

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define TRUE 1

int getNumber(void);
int digitCheck(char*);
char* intToAscii(int number);
void reverseString(char* input);

int main()
{
  int number = 0;
  int add = 0;
  int i;
 
  printf("add two numbers\n\n");

  for(i=1; i<2; i++)
  {
     number = getNumber();
     puts(intToAscii(number));
     puts(intToAscii(number));
  }
  
  //add = number1 + number2;

  //printf("\nANS: %s + %s = %d\n\n", n1, n2, add);
  return 0;
}

/*
function getNumber
gets a numeric string from the user and returns the integer
representation of the string
*/
int getNumber(void)
{
  char input[16];
  int  number = 0;
  int  inputLen;
  int  i;

  while (TRUE)
  {
      printf("Input a number <0-99>: ");
      gets(input);

      if (digitCheck(input) == 0)
        break;

      printf("ERROR: invalid number entered. Please Re-Enter.\n\n");
  }

  inputLen = strlen(input);
  for (i = 0; i < inputLen; i++)
  {
      int digit = input[i] - '0';
      number *= 10;
      number += digit;
  }

  return number;
}

/*
function digitCheck
checks that input is all digits and checks that input length is 1 or 2
returns 0 if the input is good
returns 1 if the input is bad
*/
int digitCheck(char* input)
{
  int i, inputLen;

  inputLen = strlen(input);
  if (inputLen< 1 || inputLen>2)
     return 1;

  for (i = 0; i < inputLen; i++)
  {
      if (input[i]<'0' || input[i]>'9')
          return 1;
  }
  return 0;
}

/*function intToAscii
reads the integer and returns the ascii
representation of the integer
*/
char* intToAscii(int number)
{
  static char array[20];
  int digit;
  char character;
  int counter = 0;

  do
  {
      digit = number % 10;
      number /= 10;
      character = digit + '0';
      array[counter] = character;
      counter++;
  } while(number != 0);

  array[counter] = '\0';

  reverseString(array);
  return array;
}


/*function reverseString
reads output from function intToAscii
and corrects the output
*/
void reverseString(char* input)
{
  int i, j;
  char t;
  
  i = strlen(input)-1;
  i = i / 2;
  
  for(j=0; j<=i; j++)
  {
      t = input[j];
      input[j] = input[i-j];
      input[i-j] = t;
  }
}

Answer
Hello Mike.

I am so very sorry. I sent you bad code for the reverseString function. I know I tried to test the code, but I must have read the bad output as being correct.

Here is the correct code.

void reverseString(char* input)
{
   int i, j;
   char t;
   
   int len = strlen(input);
   i = len / 2;

   for(j=0; j < i; j++)
   {
       t = input[j];
       input[j] = input[len-j-1];
       input[len-j-1] = t;
   }
}

There is no problem with using the putchar or puts on the character array.

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Zlatko

Expertise

No longer taking questions.

Experience

No longer taking questions.

Education/Credentials
No longer taking questions.

©2012 About.com, a part of The New York Times Company. All rights reserved.