Thread: Converting Integer to ASCII

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    14

    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!
    Last edited by Kurdo; 01-08-2011 at 09:29 AM. Reason: explained better now

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Why do you think there is an ASCII symbol for "135"? Do you mean convert an integer to a string?

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    14
    Quote Originally Posted by tabstop View Post
    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. #4
    Registered User \007's Avatar
    Join Date
    Dec 2010
    Posts
    179
    printf("%c", 135);

    Printf can do the conversion on its own.

  5. #5
    Registered User
    Join Date
    Dec 2010
    Posts
    14
    Quote Originally Posted by \007 View Post
    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. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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. #7
    Registered User \007's Avatar
    Join Date
    Dec 2010
    Posts
    179
    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. #8
    Registered User \007's Avatar
    Join Date
    Dec 2010
    Posts
    179
    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. #9
    Registered User
    Join Date
    Dec 2010
    Posts
    14
    can you read my question again, i've edited it. I hope it more clear now.

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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. #11
    Registered User \007's Avatar
    Join Date
    Dec 2010
    Posts
    179
    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. #12
    Registered User
    Join Date
    Dec 2010
    Posts
    14
    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;
    
        isaddop (*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;
    
    
    }
    Thanks again for the reply

  13. #13
    Registered User \007's Avatar
    Join Date
    Dec 2010
    Posts
    179
    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. #14
    Registered User
    Join Date
    Dec 2010
    Posts
    14
    Ok, thanks i'm gonna try it out.

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You can't have n be both a char and "the value of the number pointed to by p". Pick one.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help assignment due tomorrow
    By wildiv in forum C Programming
    Replies: 6
    Last Post: 01-27-2010, 08:38 PM
  2. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  3. Lame null append cause buffer to crash
    By cmoo in forum C Programming
    Replies: 8
    Last Post: 12-29-2008, 03:27 AM
  4. Replies: 3
    Last Post: 01-25-2006, 08:04 PM
  5. Replies: 3
    Last Post: 05-03-2002, 05:18 PM

Tags for this Thread