# Thread: Converting Integer to ASCII

1. ## Converting Integer to ASCII

Hi,

I have to program a parser that parses expressions. The input is a string (an expression). I have to make a function called get_number. It checks if there are numbers in the string. Also to simplify the string, i have to use a pointer that points to the input string. So i have to check if there are numbers in the string, by using the pointer. If there is at least one number in the string, i have to convert that to a number in the ASCII representation.

If somebody knows how to do it, I would love to know.

Thanks!

2. Why do you think there is an ASCII symbol for "135"? Do you mean convert an integer to a string?

3. Originally Posted by tabstop
Why do you think there is an ASCII symbol for "135"? Do you mean convert an integer to a string?
no, that was just a random integer. I have to show a number in the ASCII representation.

4. printf("%c", 135);

Printf can do the conversion on its own.

5. Originally Posted by \007
printf("%c", 135);

Printf can do the conversion on its own.
the 135 was just a random number. Let's forget that i said that :P.

This assignment is for a parser. The input is some expression. I have to find the numbers in the expression and convert them to ASCII.

6. If you read in input, then you have ASCII characters (assuming you're not on an EBCDIC machine somehow). Are you sure your difficulty isn't going the other way (you have ASCII and you want the value of the number instead)?

7. itoa(3) - Linux man page

Or just use printf and convert it on the fly. It will work. I used a similar method in a Caesar Cipher which I posted up on my blog.

8. By the way, C doesn't really care when it comes to ints or chars, a char is just a lesser int anyway. You can print out the char representation of any int, or any expression which evaluates to an int.

9. can you read my question again, i've edited it. I hope it more clear now.

10. We understand just fine. We are trying to get you to understand: when you read in a character, YOU HAVE THE ASCII REPRESENTATION OF THAT CHARACTER. Stop asking how to get the ASCII representation, because that's what you have. You may well want to convert out of the ASCII representation.

Anyway, you should look at isdigit() and strtol().

11. Man Page for isnumber (FreeBSD Section 3) - The UNIX and Linux Forums

isalpha(3): char classification routines - Linux man page

strtok(3): extract tokens from strings - Linux man page

Can run through it and check every character after tokenizing it or something, if it's a number do something you want with it. You will probably end up checking it character by character though. Those functions may be helpful.

12. Ok, you you mean i can print the *p to get the ASCII representation?

Also i am using isdigit atm.

Sorry if seemed a bit rude, that wasnt the intention.

This is what i have now:

Code:
```int get_number()
{
char *p, n, result;

n = *p;

if (isdigit (*p)!=0)
result = TRUE;
else
{
printf("Error: expected digit");
exit;
}

while(isdigit(*p)!=0)
{
n = n * 10 + 48;/* it goes wrong from here*/

n++;
}

return n;

}```

13. If *p is a digit then yes you can print the ascii or the digit representation.

Could be done like:

printf("%c", *p);

As long as *p was a digit or a char. I didn't test it, worst case you would have to slap a conversion around it like:

printf("%c", atoi(*p));

That is a bit redundant though.

Again, I didn't test or read your code. I just know you can make it work.

14. Ok, thanks i'm gonna try it out.

15. You can't have n be both a char and "the value of the number pointed to by p". Pick one.