Thread: how to convert decimal number to ASCII code?

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    3

    how to convert decimal number to ASCII code?

    Hi all, can anyone help me. I need to convert decimal number to ASCII code. AS i am currently working on a project than require me to display my result onto a LCD display. As LCD display is using ASCII code to display. Anyone know please help me. THANKSSSsss...

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You mean like entering a number and displaying the appropriate letter or character? Why do you need to convert anything?
    Code:
    printf("%c", 100 );
    Just display it appropriately.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    What exactly is your question? Do you need to convert a decimal to its ASCII equivalent (12.1 to the string 12.1)? Or do you want to convert 12.1 into 0C2E01?
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I think the OP wants to change
    Code:
    int x = 123;
    into
    Code:
    "123"
    In which case, see the FAQ: http://faq.cprogramming.com/cgi-bin/...&id=1043284385

    For the other way around, "123" -> 123: http://faq.cprogramming.com/cgi-bin/...&id=1043284385
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Try this:
    Code:
    int x = 123;
    char buf [3];
    itoa(x,buf,10);
    printf(buf);
    Be careful, however, as this is nonstandard.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    And overflows. You forgot space for the NUL.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    hehe. Whoops.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  8. #8
    Registered User
    Join Date
    Oct 2006
    Posts
    3
    I am using a microcontroller to do my project. The result i get from the microcontroller is in float and i need to get my result to be display in a LCD. I do not have a decoder connect to it. That why i need to write a program in C that will able to convert the float into ASCII code.

    For example if the result i get from my microC is 123.45. I want the LCD to display 123.45 also and not any other junk symbol. Without convert the float into ASCII it will display the result into a junk symbol. As LCD only able to recognize ASCII code. Anyone get i trying to do??

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    So why don't you just use printf?


    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User
    Join Date
    Oct 2006
    Posts
    3
    printf does not work.

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why? How? Don't just say "doesn't work". Why doesn't it work? What forms of output are you allowed? Fine. Use sprintf, and use putchar or whatever your output function is.


    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    So use sprintf() if you want to convert it to a string.

    Or write some really funky code (like what the internals of sprintf does) to convert the float into a string yourself.

    Have you ever tried to convert an integer into a string using /10 and %10 operations in a loop? Well it's a bit like that.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Lame null append cause buffer to crash
    By cmoo in forum C Programming
    Replies: 8
    Last Post: 12-29-2008, 03:27 AM
  2. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  3. Replies: 11
    Last Post: 03-24-2006, 11:26 AM
  4. Replies: 10
    Last Post: 08-17-2005, 11:17 PM
  5. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM